简体   繁体   English

UIImageView沿触摸方向旋转动画

[英]UIImageView rotation animation in the touched direction

I have one UIImageView having an image of an arrow. 我有一个具有箭头图像的UIImageView。 When user taps on the UIView this arrow should point to the direction of the tap maintaing its position it should just change the transform. 当用户在UIView上点击时,该箭头应指向点击的方向并保持其位置,只需更改转换即可。 I have implemented following code. 我已经实现了以下代码。 But it not working as expected. 但是它没有按预期工作。 I have added a screenshot. 我添加了屏幕截图。 In this screenshot when i touch the point upper left the arrow direction should be as shown.But it is not happening so. 在此屏幕截图中,当我触摸左上角的点时,箭头方向应如图所示。但是事实并非如此。

-(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;
}

在此处输入图片说明

I assume you wanted an arrow image to point to where ever you touch, I tried and this is what i could come up with. 我假设您想将箭头图像指向您触摸的任何地方,我尝试过,这就是我能想到的。 I put an image view with an arrow pointing upwards (haven't tried starting from any other position, log gives correct angles) and on touching on different locations it rotates and points to touched location. 我将图像视图的箭头指向上方(未尝试从任何其他位置开始,日志给出了正确的角度),并在触摸不同位置时旋转并指向触摸位置。 Hope it helps ( tried some old math :-) ) 希望对您有所帮助(尝试了一些旧的数学方法:-))

-(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 -anoop4real

Given what you told me, I think the problem is that you are not resetting your transform in touchesBegan . 根据您告诉我的内容,我认为问题在于您没有在touchesBegan重置转换。 Try changing it to something like this and see if it works better: 尝试将其更改为如下所示,然后查看是否效果更好:

-(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 ;
}

Do you need the line to "remove the discontinuity"? 您是否需要该行来“消除不连续性”? Seems atan2f() returns values between +π to -π. 似乎atan2f()返回的值介于+π到-π之间。 Won't those work directly with CATransform3DMakeRotation() ? 这些不是直接与CATransform3DMakeRotation()吗?

What you need is that the arrow points to the last tapped point. 您需要的是箭头指向最后一个点击的点。 To simplify and test, I have used a tap gesture (but it's similar to a touchBegan:withEvent:). 为了简化和测试,我使用了轻击手势(但它类似于touchBegan:withEvent :)。

In the viewDidLoad method, I register the gesture : 在viewDidLoad方法中,我注册了手势:

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

The method called on each tap : 每次点击调用的方法:

- (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);
}

One key is the + M_PI_2 because UIKit coordinates have the origin at the top left corner (while in trigonometric, we use a bottom left corner). 一键是+ M_PI_2因为UIKit坐标的原点在左上角(在三角函数中,我们使用左下角)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM