顺时针旋转UIView大于180度的角度 [英] Rotate a UIView clockwise for an angle greater than 180 degrees

查看:489
本文介绍了顺时针旋转UIView大于180度的角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将钟表臂指向12点钟到当前时间。如果是,11点,我想让手臂顺时针旋转到11点位置。不过,当然,如果我使用:

I'm animating a clock arm from pointing towards 12 o'clock to the current time. If it is say, 11 o'clock, I want the arm to rotate clockwise to the 11 o'clock position. But of course if I use:

 CGAffineTransform rotation = CGAffineTransformMakeRotation(2*M_PI*11/12);

 [UIView animateWithDuration:3.0
                  animations:^{
                         clockArm.transform = rotation;
                     }];

旋转逆时针旋转。我试过了:

the rotation goes counterclockwise. I tried:

CGFloat angle = 2*M_PI*11/12;
CGAffineTransform firstRotation = CGAffineTransformMakeRotation(M_PI-0.001);
CGFloat firstRotationTime = 3.0*(M_PI/angle);
CGAffineTransform secondRotation = CGAffineTransformMakeRotation(angle);
CGFloat secondRotationTime = 3.0 - firstRotationTime;

[UIView animateWithDuration:firstRotationTime
                      delay:0.0
                    options:UIViewAnimationCurveLinear
                 animations:^{
                         self.clockArm1.transform = firstRotation;
                     }
                 completion:^(BOOL finished) {
                     [UIView animateWithDuration:secondRotationTime
                                           delay:0.0
                                         options:UIViewAnimationCurveLinear
                                      animations:^{
                                          self.clockArm1.transform = secondRotation;
                                      }
                                      completion:^(BOOL finished){
                                      }];
                 }];

动画确实是顺时针方向,但它很不稳定 - 动画似乎仍然在做一个UIViewAnimationEaseInOut第一个动画。我做错了什么,或者有其他方法可以实现我想要的目标吗?

The animation does go clockwise, but it is choppy - the animation still seems to be doing a UIViewAnimationEaseInOut for the first animation. What am I doing wrong, or is there another way to accomplish what I want?

推荐答案

你可以使用<的完成块code> CATransaction 在动画结束时设置视图的rotation属性。以下函数在我的测试用例中起作用:

You can use the completion block of CATransaction to set the rotation property of the view when the animation has finished. The following function worked in my test case:

- (void) rotateViewAnimated:(UIView*)view
               withDuration:(CFTimeInterval)duration
                    byAngle:(CGFloat)angle
{
    [CATransaction begin];
    CABasicAnimation *rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.byValue = [NSNumber numberWithFloat:angle];
    rotationAnimation.duration = duration;
    rotationAnimation.removedOnCompletion = YES;

    [CATransaction setCompletionBlock:^{
        view.transform = CGAffineTransformRotate(view.transform, angle);
    }];

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    [CATransaction commit];
}

您可以像

[self rotateViewAnimated:self.clockArm1 withDuration:3.0 byAngle:2*M_PI*11./12.];

这篇关于顺时针旋转UIView大于180度的角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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