簡體   English   中英

UIImageView沿觸摸方向旋轉動畫

[英]UIImageView rotation animation in the touched direction

我有一個具有箭頭圖像的UIImageView。 當用戶在UIView上點擊時,該箭頭應指向點擊的方向並保持其位置,只需更改轉換即可。 我已經實現了以下代碼。 但是它沒有按預期工作。 我添加了屏幕截圖。 在此屏幕截圖中,當我觸摸左上角的點時,箭頭方向應如圖所示。但是事實並非如此。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

 UITouch *touch=[[event allTouches]anyObject];
 touchedPoint= [touch locationInView:touch.view];
 imageViews.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(rangle11));
 previousTouchedPoint = touchedPoint ;
}

- (CGFloat) pointPairToBearingDegrees:(CGPoint)startingPoint secondPoint:(CGPoint) endingPoint
{

 CGPoint originPoint = CGPointMake(endingPoint.x - startingPoint.x, endingPoint.y - startingPoint.y); // get origin point to origin by subtracting end from start
   float bearingRadians = atan2f(originPoint.y, originPoint.x); // get bearing in radians
float bearingDegrees = bearingRadians * (180.0 / M_PI); // convert to degrees
bearingDegrees = (bearingDegrees > 0.0 ? bearingDegrees : (360.0 + bearingDegrees)); // correct discontinuity
return bearingDegrees;
}

在此處輸入圖片說明

我假設您想將箭頭圖像指向您觸摸的任何地方,我嘗試過,這就是我能想到的。 我將圖像視圖的箭頭指向上方(未嘗試從任何其他位置開始,日志給出了正確的角度),並在觸摸不同位置時旋轉並指向觸摸位置。 希望對您有所幫助(嘗試了一些舊的數學方法:-))

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch=[[event allTouches]anyObject];
    touchedPoint= [touch locationInView:touch.view];
    CGFloat angle = [self getAngle:touchedPoint];
    imageView.transform = CGAffineTransformMakeRotation(angle);

}

-(CGFloat) getAngle: (CGPoint) touchedPoints
{
    CGFloat x1 = imageView.center.x;
    CGFloat y1 = imageView.center.y;

    CGFloat x2 = touchedPoints.x;
    CGFloat y2 = touchedPoints.y;

    CGFloat x3 = x1;
    CGFloat y3 = y2;

    CGFloat oppSide = sqrtf(((x2-x3)*(x2-x3)) + ((y2-y3)*(y2-y3)));
    CGFloat adjSide = sqrtf(((x1-x3)*(x1-x3)) + ((y1-y3)*(y1-y3)));

    CGFloat angle = atanf(oppSide/adjSide);
    // Quadrant Identifiaction
    if(x2 < imageView.center.x)
    {
        angle = 0-angle;
    }


    if(y2 > imageView.center.y)
    {
        angle = M_PI/2 + (M_PI/2 -angle);
    }
     NSLog(@"Angle is %2f",angle*180/M_PI);
    return angle;

}

-anoop4real

根據您告訴我的內容,我認為問題在於您沒有在touchesBegan重置轉換。 嘗試將其更改為如下所示,然后查看是否效果更好:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

 UITouch *touch=[[event allTouches]anyObject];
 touchedPoint= [touch locationInView:touch.view];
 imageViews.transform = CGAffineTransformIdentity;
 imageViews.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(rangle11));
 previousTouchedPoint = touchedPoint ;
}

您是否需要該行來“消除不連續性”? 似乎atan2f()返回的值介於+π到-π之間。 這些不是直接與CATransform3DMakeRotation()嗎?

您需要的是箭頭指向最后一個點擊的點。 為了簡化和測試,我使用了輕擊手勢(但它類似於touchBegan:withEvent :)。

在viewDidLoad方法中,我注冊了手勢:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];

每次點擊調用的方法:

- (void)tapped:(UITapGestureRecognizer *)gesture
{
    CGPoint imageCenter = mFlecheImageView.center;
    CGPoint tapPoint = [gesture locationInView:self.view];

    double deltaY = tapPoint.y - imageCenter.y;
    double deltaX = tapPoint.x - imageCenter.x;
    double angleInRadians = atan2(deltaY, deltaX) + M_PI_2;

    mFlecheImageView.transform = CGAffineTransformMakeRotation(angleInRadians);
}

一鍵是+ M_PI_2因為UIKit坐標的原點在左上角(在三角函數中,我們使用左下角)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM