ios - 如何使 UIView 动画序列重复和自动反转

标签 ios objective-c uiview uiviewanimation

如何让这个复杂的动画重复和自动反转? 有没有办法添加选项 UIViewAnimationOptionAutoreverse | UIViewAnimationOption是否重复此动画序列?

   [UIView animateWithDuration:1.0f animations:^{

        someView.frame = someFrame1;

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.5f animations:^{

            someView.frame = someFrame2;

        } completion:nil];

    }];

最佳答案

要从点 1 到 2 到 3 到 2 到 1 并重复动画,您可以在 iOS 7 及更高版本中使用 animateKeyframesWithDuration:

someView.frame = frame1;
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
    [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
        someView.frame = frame2;
    }];
    [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
        someView.frame = frame3;
    }];
} completion:nil];

如果使用自动布局,您可以动画化约束常量的变化:

[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
    [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
        topConstraint.constant = 200;
        leftConstraint.constant = 200;
        [self.view layoutIfNeeded];
    }];
    [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
        topConstraint.constant = 100;
        leftConstraint.constant = 300;
        [self.view layoutIfNeeded];
    }];
} completion:nil];

或者,自动布局的方法是停用约束,然后您可以使用 frame 值或您拥有的值来制作动画。


在早期版本的 iOS 中,您可以使用 CAKeyframeAnimation,例如沿着路径设置动画:

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100.0, 100.0)];
[path addLineToPoint:CGPointMake(200.0, 200.0)];
[path addLineToPoint:CGPointMake(100.0, 300.0)];

CAKeyframeAnimation *animatePosition = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animatePosition.path = [path CGPath];
animatePosition.duration = 1.0;
animatePosition.autoreverses = YES;
animatePosition.repeatCount = HUGE_VALF;
[self.someView.layer addAnimation:animatePosition forKey:@"position"];

您可以用任意多的点来做到这一点。如果您想沿着弯曲的路径(例如圆形或贝塞尔曲线)制作动画,这也是有用的技术。


要在两点之间设置动画,您可以使用animateWithDuration:delay:options:animations:completion:,例如:

[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     // do whatever animation you want, e.g.,

                     someView.frame = someFrame1;
                 }
                 completion:NULL];

这为 someView 从起始帧到 someFrame1 并返回的动画。

顺便说一下,将 UIViewAnimationOptionCurveEaseInOutUIViewAnimationOptionAutoreverseUIViewAnimationOptionRepeat 结合使用会给您带来更流畅的动画反转和重复效果。

关于ios - 如何使 UIView 动画序列重复和自动反转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14730460/

相关文章:

ios - 发送应用程序崩溃日志/常规应用程序日志

ios - 带有 SpriteKit 的 UITableView

ios - 我将如何为 BringSubviewToFront 编写 UIView 淡入动画

ios - 糟糕的 UITableView 滚动性能问题 iOS 7

iphone - 添加 subview 然后释放它: "modifying view that is being finalized"

ios - 在 AVMutableVideoComposition 中创建一个按时间间隔更改的 CATextlayer

ios - Swift scaleAspectFill 只有宽度

ios - 在 Swift 中无限期振动

iphone - 当 View 包含带有自定义图像的 UIButton 时,UIViewControllerencodeWithCoder 失败

iphone - iPad的iAd何时可用?