ios - 如何使用 CABasicAnimations 和自动布局替换 CGAffineTransform 缩放和 alpha 动画?

标签 ios animation calayer cabasicanimation

据我所知,Apple 希望我们从 CGAffineTransform 动画转向使用以下动画:

myView.layoutConstraint.constant = someNewValue;

[myView layoutIfNeeded];

用于涉及 View 转换的动画。

我们现在似乎也应该使用 CABasicAnimation 动画来缩放和旋转(有时还包括不透明度),因为它为 View 的图层而不是 View 设置动画,并且在这样做时,可以很好地配合自动布局。

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

[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来做缩放。您可以像在问题顶部提到的那样进行操作。为 View 的宽度和高度约束常量值设置一个新值,然后在 animateWithDuration 中使用 [myView layoutIfNeeded]。如果 View 没有高度和宽度约束,但父 View 的顶部和底部和/或左右边缘具有常量,请改为更改这些值。

关于ios - 如何使用 CABasicAnimations 和自动布局替换 CGAffineTransform 缩放和 alpha 动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18454986/

相关文章:

ios - 如何使用 subview 调整 UIView 的大小

ios - Swift:解释 NSURLSession.sharedSession().dataTaskWithURL 的语法语法

ios - NStimer 和 setNeedsDisplay

iphone - UITableView 自定义节标题,重复问题

javascript - 如何制作这个加载动画?

iphone - 动画 UIView 及其子层

core-animation - 如何获得转换后的 CALayer 的实际大小?

ios - SDK 'Application' 中的产品类型 'iOS 11.2' 需要代码签名

jquery动画循环问题

iphone - 将子图层添加到 view.layer 会改变框架位置吗?