0

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

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

我究竟做错了什么?

// 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];
4

2 回答 2

0

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

于 2013-09-14T17:46:39.740 回答
0

我使用以下代码让它工作。我的视图对超级视图的左侧有一个约束,我在其中添加了 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];
     }
}
于 2013-09-14T19:46:29.500 回答