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

问题描述:

据我所看到的,苹果希望我们从CGAffineTransform动画和动画将使用移开:如何使用CABasicAnimations和Auto Layout替换CGAffineTransform缩放和alpha动画?

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

}]; 

我不知道你为什么想用CABasicAnimation来做它的缩放。你可以像你在问题顶部提到的那样去做。为视图的宽度和高度约束常量值设置一个新值,然后在animateWithDuration内使用[myView layoutIfNeeded]。如果视图不具有高度和宽度约束,但对超视图的顶部和底部和/或左侧和右侧边缘具有常量,请改为改变这些值。

+0

我不一定想为此使用CABasicAnimation,但没有看到功能的替代方案。如果我使用UIButton上的高度/宽度约束调整,我认为它可能只是调整按钮的框架大小(背景清晰且不可见),而不是文本本身。这给了我一个想法,但我想我可以只是改变按钮的字体大小和动画。有点破解,但我会尝试并报告回来。 –

+0

更改字体大小不起作用。该按钮在动画改变时四处移动。 –