繁体   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