CGAffineTransformMakeRotation始终逆时针方向

Sam*_*m B 8 iphone xcode ipad ios

UIImageView当用户触摸它并沿顺时针方向将其手指移动时,我试图动画a 以逆时针方向旋转.
基本上我希望UIImageView一旦触摸结束就回到原来的位置,它应该以逆时针方向移动.

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

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

注意:这里的角度变量是一个double,它的初始值为零,在整个代码中它保持为0

-(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);
        }];
} 
Run Code Online (Sandbox Code Playgroud)

我试着查看这篇文章,但它有几个问题

  • 动画完成后,我无法再触摸和旋转图像
  • 它始终以顺时针方向动画,我无法弄清楚如何从用户结束旋转图像的位置顺时针逆时针方向移动.

Dar*_*ren 3

我遇到了类似的问题,查看已接受的答案,它可能对您有帮助:

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


针对评论,也许这会有所帮助:

在我的代码中我实际使用

rotationAnimation.fromValue
rotationAnimation.toValue
Run Code Online (Sandbox Code Playgroud)

代替

rotationAnimation.byValue.
Run Code Online (Sandbox Code Playgroud)

如果 toValue 小于 fromValue,则旋转始终为逆时针。你的价值观是积极的还是消极的并不重要,重要的是它们之间的关系。

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

-(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"];
}
Run Code Online (Sandbox Code Playgroud)