如何使用CABasicAnimations和Auto Layout替换CGAffineTransform缩放和alpha动画?

时间:2013-08-27 00:06:18

标签: ios animation calayer cabasicanimation

据我所见,Apple希望我们使用以下功能从CGAffineTransform动画转移到动画中:

myView.layoutConstraint.constant = someNewValue;

[myView layoutIfNeeded];

用于涉及视图翻译的动画。

似乎我们现在应该使用CABasicAnimation动画进行缩放和旋转(有时是不透明度),因为它动画了视图的图层而不是视图,这样做可以很好地与自动布局配合使用。

我使用以下代码来应用效果非常好的不透明度和缩放动画:

[UIView animateWithDuration:0.1f animations:^{
//        first animation
self.myMeButton.alpha = 1;
self.myMeButton.transform = CGAffineTransformMakeScale(1.1, 1.1);

} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.2f animations:^{
        //        second animation
        self.myButton.transform = CGAffineTransformMakeScale(1, 1);
    }];
}];

当然,自动布局会对缩放动画造成严重破坏,所以我试图找到另一种方法来做到这一点。到目前为止,我已经想出了这个:

    [CATransaction begin]; {
    [CATransaction setCompletionBlock:^{
        //            code for when animation completes
        self.pickMeButton.alpha = 1;

        CABasicAnimation *scaleDown = [CABasicAnimation animationWithKeyPath:@"scale"];
        scaleDown.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1)];
        scaleDown.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)];
        scaleDown.duration = 0.1;

        [self.myButton.layer addAnimation:scaleDown forKey:nil];

    }];
    //  describe animations:
    CABasicAnimation* scaleUp = [CABasicAnimation animationWithKeyPath:@"scale"];
    scaleUp.autoreverses = NO;
    scaleUp.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)];
    scaleUp.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1)];

    CABasicAnimation *fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadeAnim.fromValue=[NSNumber numberWithDouble:0.0];
    fadeAnim.toValue=[NSNumber numberWithDouble:1.0];

    // Customization for all animations:
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.duration = 0.2f;
    group.repeatCount = 1;
    group.autoreverses = NO;
    group.animations = @[scaleUp, fadeAnim];

    // add animations to the view's layer:
    [self.myButton.layer addAnimation:group forKey:@"allMyAnimations"];


} [CATransaction commit];

正如您所看到的那样,代码几乎是之前的3倍,并且设备上的动画明显不如以前那么“平滑”。

有没有办法更好地做到这一点?

提前感谢您的回复。

编辑:这似乎已经成功了,动画很流畅,但我仍然觉得这个代码可以更加简洁。

[UIView animateWithDuration:0.2f animations:^{

    self.pickMeButton.alpha = 1;

    CABasicAnimation* scaleUp = [CABasicAnimation animationWithKeyPath:@"transform"];
    scaleUp.duration = 0.2;
    scaleUp.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1)];
    scaleUp.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1)];
    [self.pickMeButton.layer addAnimation:scaleUp forKey:nil];

}completion:^(BOOL finished) {

    CABasicAnimation* scaleDown = [CABasicAnimation animationWithKeyPath:@"transform"];
    scaleDown.duration = 0.1;
    scaleDown.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1)];
    scaleDown.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)];
    [self.pickMeButton.layer addAnimation:scaleDown forKey:nil];

}];

1 个答案:

答案 0 :(得分:2)

我不知道你为什么要用CABasicAnimation进行比例缩放。你可以像在问题的顶部提到的那样做。为视图的宽度和高度约束常量值设置新值,然后在animateWithDuration中使用[myView layoutIfNeeded]。如果视图没有高度和宽度约束,但是对于超视图的顶部和底部和/或左右边缘具有常量,请改为更改这些值。