CGAffineTransformMakeRotation始终逆时针方向 [英] CGAffineTransformMakeRotation counter clockwise always

查看:440
本文介绍了CGAffineTransformMakeRotation始终逆时针方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动画 UIImageView 在用户触摸时逆时针旋转并按顺时针方向移动手指。

基本上我希望一旦触摸结束,UIImageView就会回到原来的位置,它应该以逆时针方向移动。

I an trying to animate a UIImageView to rotate counter clockwise when a user touches it and moves its finger on it in clockwise direction.
Basically I want the UIImageView to go back to its original position once the touch ends and it should move in counter clockwise direction.

我遇到的问题是,每当角度(以度为单位)大于180时,图像顺时针旋转回原位。它显然走回了最短路径。对于179度或更小的角度,图像逆时针旋转(我需要的)。

The problem I have is that every time angle (in degrees) is greater than 180 then the image rotates clockwise back to its original position. Its apparently taking the shortest path back. As for angles 179 degrees or less the image rotate counter clockwise (what i need).

我尝试了这两段代码并且它们的行为方式相同。有什么建议吗?

I tried both these pieces of code and they behave the same way. Any suggestions?

注意:这里的角度变量是一个双倍,它的初始化为零,在我的代码中它保持为0

NOTE: here angle variable is a double and its initialized to be zero and it remains 0 throughout my code

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [self handleObject:touches withEvent:event isLast:YES];

    /* this doesn't make rotation counter clockwise for rotations greater than 180 */
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve: UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationRepeatCount:1]; 
    CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
    circle1ImgVw.transform = CGAffineTransformRotate(transform, angle);
    // ends animation
    [UIView commitAnimations];


    /* this doesn't make rotation counter clockwise for rotations greater than 180 */
    [UIView animateWithDuration:1.0 animations:^{
            circle1ImgVw.transform = CGAffineTransformMakeRotation(angle);
        }];
} 

我试过调查这篇文章并且它有几个问题

I tried looking into this post and there are couple of problems with it


  • 动画完成后我无法再触摸并旋转图像

  • 它始终以顺时针方向动画而且我无法弄清楚如何从用户结束旋转图像的位置顺时针顺时针方向移动它。

推荐答案

我有类似的问题,检查接受的答案,它可能对你有帮助:

I had a similar issue, check the accepted answer out, it might help you:

顺时针旋转UIView大于180度的角度

在回复评论时,这可能会有所帮助:

In response to comments, maybe this will help:

在我的代码中我实际上使用

In my code I actually use

rotationAnimation.fromValue
rotationAnimation.toValue

而不是

rotationAnimation.byValue.

如果toValue小于fromValue,则旋转总是逆时针旋转。无论你的价值是积极的还是消极的,只考虑它们之间的关系。

The rotation is always going to be counter clockwise if the toValue is less than the fromValue. It doesn't matter if your values are positive or negative, only the relation between them.

找出你的起始角度是什么,找出你想要旋转的方向并找出你的结束角度是什么。如果你想逆时针方向,请确保它小于你的起始角度。这是我用来顺时针从12:00到当前时间动画时钟指针的代码。

Figure out what your starting angle is, figure out which direction you want to rotate and figure out what your ending angle is. Make sure that it's less than your starting angle if you want to go counter clockwise. Here is the code I use for animating a clock hand clockwise from 12:00 to the current time.

-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self updateClockArmAngle];
}

- (void)updateClockArmAngle
{
    // Get the time
    NSDate *date = [NSDate date];
    NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"America/Toronto"]];
    NSDateComponents* components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date];

    CGFloat hour = [components hour];
    CGFloat angle = (hour/24.0)*(2*M_PI);

    CGFloat minute = [components minute];
    angle += (minute/(24*60))*(2*M_PI);

    [self rotateViewAnimated:self.clockArm withDuration:1.5 byAngle:angle];
}


- (void) rotateViewAnimated:(UIView*)view
               withDuration:(CFTimeInterval)duration
                    byAngle:(CGFloat)angle
{    
    CABasicAnimation *rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.fromValue = 0;
    rotationAnimation.toValue = [NSNumber numberWithFloat:angle];
    rotationAnimation.duration = duration;
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [rotationAnimation setRemovedOnCompletion:NO];
    [rotationAnimation setFillMode:kCAFillModeForwards];
    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

这篇关于CGAffineTransformMakeRotation始终逆时针方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆