0

正方形の画像や円形の画像を変換するのは簡単です...

しかし、長方形の画像の場合はどうなりますか?

これは、タッチイベントで変換したい画像です。

ここに画像の説明を入力してください

画面上の任意の場所に触れると、円の中心は(30、236)になります

Imageviewは矢印を私のタッチポイントに変換します。

しかし、円の中心はまだ同じ場所です。

テスト角度を使用して画像を変換しようとします

こんな感じになります...

ここに画像の説明を入力してください

画像を調整する方法は?

また、コードはここにあります

- (void)viewDidLoad {
    CGRect arrowImageRect = CGRectMake(20.0f, 20.0f, 15.0f, 220.0f);
    arrow = [[UIImageView alloc] initWithFrame:arrowImageRect];
    [arrow setImage:[UIImage imageNamed:@"arrow.png"]];
    arrow.opaque = YES;
    [self.view addSubview:arrow];
    [super viewDidLoad];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
    NSLog(@"Position X: %f \n, Y: %f",touchPoint.x,touchPoint.y);
    CGAffineTransform transform = CGAffineTransformIdentity;
    arrow.transform = CGAffineTransformRotate(transform, ?????? );
}

?????? 一部は最後の解決策でなければなりません...

または多分私はimageviewのサイズを変更する必要があります???

返信や回答をありがとう:-)

ウェバー

4

3 に答える 3

2

これが私がこの問題を理解する最終的な解決策です

このコードを確認してください

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[[event allTouches]anyObject];

    [self transformSpinnerwithTouches:touch];
}

-(void)transformSpinnerwithTouches:(UITouch *)touchLocation
{
    CGPoint touchLocationpoint = [touchLocation locationInView:self.view];
    CGPoint PrevioustouchLocationpoint = [touchLocation previousLocationInView:self.view];
    //origin is the respective piont from that i gonna measure the angle of the current position with respective to previous position ....
    CGPoint origin;
    origin.x = arrow.center.x;
    origin.y = arrow.center.y;
    CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint];
    CGAffineTransform newTransform = CGAffineTransformScale(arrow.transform, 1, 1);
    CGFloat previousRotation = atan2(previousDifference.y, previousDifference.x);
    CGPoint currentDifference = [self vectorFromPoint:origin toPoint:touchLocationpoint];
    CGFloat currentRotation = atan2(currentDifference.y, currentDifference.x);
    CGFloat newAngle = currentRotation- previousRotation;
    temp1 = temp1 + newAngle;
    //NSLog(@"temp1 is %f",temp1);
    //NSLog(@"Angle:%F\n",(temp1*180)/M_PI);
    newTransform = CGAffineTransformRotate(newTransform, newAngle);
    [self animateView:arrow toPosition:newTransform];
}
-(void)animateView:(UIView *)theView toPosition:(CGAffineTransform) newTransform
{
arrow.transform = newTransform;
}

-(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint
{
    CGFloat x = secondPoint.x - firstPoint.x;
    CGFloat y = secondPoint.y - firstPoint.y;
    CGPoint result = CGPointMake(x, y);
    //NSLog(@"%f %f",x,y);
    return result;
}
于 2011-11-04T04:09:47.717 に答える
0

変換メソッドに関するドキュメントから:

変換の原点は、centerプロパティの値、または変更された場合はレイヤーのanchorPointプロパティです。(layerプロパティを使用して、基になるCore Animationレイヤーオブジェクトを取得します。)デフォルト値はCGAffineTransformIdentityです。

したがって、この方法で回転の中心を設定できるはずです。

また、回転角を決定するには、atan2oratan2f関数を使用します。

float angleInRadians = atan2f(touchPoint.y-circleCenter.y , touchPoint.x-circleCenter.x);
于 2011-09-07T08:23:14.983 に答える
0

変換後に中心を再設定できます。

CGPoint centerBeforeTransform = view.center;
//your transform code
view.center = centerBeforeTransform;
于 2011-09-07T11:00:45.153 に答える