同时动画旋转和视图位置

时间:2013-09-14 17:42:06

标签: ios uiview uiviewanimation

旋转视图并同时移动其位置的最佳方法是什么?

我希望此视图在向左滑动时旋转1整圈,然后在向后滑动到其原始位置时旋转另一个方向。我得到了这个工作的第一部分(向左滑动时旋转),但是当我向右滑动时尝试旋转时,视图跳回原来的位置而没有动画。

我做错了什么?

// step 1 (this works)
[UIView animateWithDuration:0.3f animations:^{
        self.number.transform = CGAffineTransformMakeRotation(DegreesToRadians(180));
        self.number.transform = CGAffineTransformMakeTranslation(-160, 0);
        self.number.center = CGPointMake(self.number.center.x - 160, self.number.center.y);
    } completion:nil];

// step 2 (this doesn't work)
[UIView animateWithDuration:0.2f animations:^{
        self.number.transform = CGAffineTransformMakeRotation(DegreesToRadians(180));
        self.number.transform = CGAffineTransformMakeTranslation(160, 0);
        self.number.center = CGPointMake(self.number.center.x + 160, self.number.center.y);
    } completion:nil];

2 个答案:

答案 0 :(得分:0)

您可能希望将第二个动画作为完成块放在第一个动画中。

答案 1 :(得分:0)

我使用以下代码使其工作。我的视图对superview的左侧有一个约束,我添加了IBOutlet,leftCon。它的初始值为160,因此平移将其一直移动到屏幕的左边缘。

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#define animationTime 1.0

@interface ViewController ()
@property (nonatomic) CGFloat leftConstant;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.leftConstant = self.leftCon.constant;
}


- (IBAction)rotate:(UIButton *)sender {
     if (self.leftCon.constant == self.leftConstant) {
        [self.view removeConstraint:self.leftCon];
        CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.toValue = @(-M_PI * 2.0);
        rotationAnimation.duration = animationTime;

        CABasicAnimation *translationAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
        translationAnimation.fromValue = [NSValue valueWithCGPoint:self.number.center];
        translationAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.number.frame.size.width/2.0, self.number.center.y)];
        translationAnimation.duration = animationTime;
        [self.number.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
        [self.number.layer addAnimation:translationAnimation forKey:@"translationAnimation"];
        self.leftCon.constant = 0;
        [self.view addConstraint:self.leftCon];
     }else{
         [self.view removeConstraint:self.leftCon];
         CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
         rotationAnimation.toValue = @(M_PI * 2.0);
         rotationAnimation.duration = animationTime;

         CABasicAnimation *translationAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
         translationAnimation.fromValue = [NSValue valueWithCGPoint:self.number.center];
         translationAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.leftConstant+ self.number.frame.size.width/2.0, self.number.center.y)];
         translationAnimation.duration = animationTime;
         [self.number.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
         [self.number.layer addAnimation:translationAnimation forKey:@"translationAnimation"];
         self.leftCon.constant = self.leftConstant;
         [self.view addConstraint:self.leftCon];
     }
}