1

据我所知,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];

}];
4

1 回答 1

2

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

于 2013-08-27T00:15:50.690 回答