【问题标题】:Animation for object对象动画
【发布时间】:2026-01-11 09:50:02
【问题描述】:

我在 Objective-c 上编程。我可以使用 CABasicAnimation 为同时使用 2 个动画的对象创建动画吗?例如,物体同时在缩放和晃动。

【问题讨论】:

    标签: objective-c ios animation cabasicanimation


    【解决方案1】:

    当然。实际上,您可以使用 CAKeyFrame、CABasic 甚至 UIView 动画来实现。这应该很好用modified from this answer

    - (void)earthquake:(UIView*)itemView
    {
        CGFloat t = 2.0;
    
        CGAffineTransform leftQuake  = CGAffineTransformTranslate(CGAffineTransformIdentity, t, -t);
        CGAffineTransform rightQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, t);
    
        itemView.transform = leftQuake;  // starting point
    
        [UIView beginAnimations:@"earthquake" context:itemView];
        [UIView setAnimationRepeatAutoreverses:YES]; // important
        [UIView setAnimationRepeatCount:5];
        [UIView setAnimationDuration:0.07];
        [UIView setAnimationDelegate:self];
        itemView.transform = rightQuake; // end here & auto-reverse
        [UIView commitAnimations];
        [UIView animateWithDuration:1.00 animations:^{
             itemView.alpha = 0.0f;
        }];
    }
    

    【讨论】:

    • 如果有人能正确格式化我的代码,我将非常感谢我在 iPhone 上输入此内容。