使用嵌套动画块的连续动画

发布于 2024-11-03 10:07:37 字数 1493 浏览 0 评论 0原文

我正在寻找一种使用嵌套动画块实现连续动画的方法。

发生在 UIScrollView 内部的情况有些复杂,三个 UIImageView 的大小(有很多图像,当我滚动它们时,我不断地交换 UIImageView 中的图像)。

滚动完成后,我想切换(可见)中间 UIImageView 中的图像三次,然后返回原始视图。我正在这样尝试:

- (void) doAnimation {
    // get the animation frames, along with the current image
    NSString *swap1 = @"first.png";
    NSString *swap2 = @"second.png";
    UIImage *original = currentPage.image;

    UIViewAnimationOptions myOptions = UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction;
    [UIView animateWithDuration:2.0 delay:2.0 options:myOptions
            animations:^{ [currentPage setImage:[UIImage imageNamed:swap1]]; }
            completion:^(BOOL finished) {
                [UIView animateWithDuration:2.0 delay:2.0 options:myOptions
                        animations:^{ [currentPage setImage:[UIImage imageNamed:swap2]]; }
                        completion:^(BOOL finished) {
                            [UIView animateWithDuration:2.0 delay:2.0 options:myOptions
                                    animations:^{ [currentPage setImage:[UIImage imageNamed:swap1]]; }
                                    completion:^(BOOL finished) {
                                        [currentPage setImage:original]; }]; }]; }];
}

当我运行这个时,没有持续时间,没有延迟,这一切都立即发生,几乎快到肉眼无法看到。这可能是因为“currentPage”是一个 UIImageView 吗? (类似于这个问题?)

I'm looking for a way to implement consecutive animations using nested animation blocks.

Somewhat complicated by happening inside a UIScrollView, the size of three UIImageViews (there are many images, and as I scroll through them I constantly swapping out the images in the UIImageViews).

When a scroll is finished, I want to switch out the image in the (visible) middle UIImageView, three times, then back to the original view. I'm trying it thus:

- (void) doAnimation {
    // get the animation frames, along with the current image
    NSString *swap1 = @"first.png";
    NSString *swap2 = @"second.png";
    UIImage *original = currentPage.image;

    UIViewAnimationOptions myOptions = UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction;
    [UIView animateWithDuration:2.0 delay:2.0 options:myOptions
            animations:^{ [currentPage setImage:[UIImage imageNamed:swap1]]; }
            completion:^(BOOL finished) {
                [UIView animateWithDuration:2.0 delay:2.0 options:myOptions
                        animations:^{ [currentPage setImage:[UIImage imageNamed:swap2]]; }
                        completion:^(BOOL finished) {
                            [UIView animateWithDuration:2.0 delay:2.0 options:myOptions
                                    animations:^{ [currentPage setImage:[UIImage imageNamed:swap1]]; }
                                    completion:^(BOOL finished) {
                                        [currentPage setImage:original]; }]; }]; }];
}

When I run this, there is no duration, no delay, it all happens at once, almost too fast for the eye to see. Could this be because "currentPage" is a UIImageView? (Similar to this question?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

马蹄踏│碎落叶 2024-11-10 10:07:37

没有延迟,因为 UIImageView.image 不是可动画的属性。因此,UIView 动画机制将不需要设置动画,只会立即调用完成块。

你期待什么类型的动画?您可以将 CATransition 对象附加到底层来获得简单的交叉淡入淡出,只需使用 [imageView.layer addAnimation:[CATransitionanimation] forKey:nil] 即可获得使用默认值的交叉淡入淡出(您可以在将 CATransition 附加到图层之前通过修改 CATransition 的属性来自定义计时)。要实现后续动画,您可以使用 CAAnimation(CATransition 的超类)的 delegate 属性来了解它何时完成并触发您的动画。第二个,或者您可以使用 -performSelector:withObject:afterDelay: 在用户定义的延迟后开始下一个动画步骤。委托方法在计时方面会更加准确,但 PerformSelector 方法更容易编写。遗憾的是,CAAnimation 不支持完成块。

There's no delay because UIImageView.image isn't an animateable property. As such, the UIView animation machinery will have no animations to set up and will just call your completion block immediately.

What sort of animation did you expect? You can attach a CATransition object to the underlying layer to get a simple cross-fade, Just use [imageView.layer addAnimation:[CATransition animation] forKey:nil] to get the crossfade with the default values (you can customize the timing by modifying properties of the CATransition before attaching it to the layer). To achieve the subsequent animations, you can either use the delegate property of CAAnimation (CATransition's superclass) to learn when it's done and fire your second one, or you could just use -performSelector:withObject:afterDelay: to start your next animation step after a user-defined delay. The delegate method is going to be more accurate with regards to timing, but the performSelector method is a bit easier to write. Sadly, CAAnimation doesn't support a completion block.

彻夜缠绵 2024-11-10 10:07:37

从一个图像视图转换到另一个图像视图的另一种方法是使用块动画函数 transitionFromView:toView:duration:options:completion 如“在视图之间创建动画过渡"。您可以这样做而不是 animateWithDuration 来更改图像。

Another approach for you to transition from one image view to another is by using the block animation function transitionFromView:toView:duration:options:completion as discussed in "Creating Animated Transitions Between Views". You would do this instead of animateWithDuration to change images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文