OStack程序员社区-中国程序员成长平台

标题: ios - 完成 objective-c 中的一项后的背靠背动画 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 19:43
标题: ios - 完成 objective-c 中的一项后的背靠背动画

我想在 3 个 ImageView 中连续制作动画,我使用了下面的代码,但它只在第一个 ImageView 中显示动画。我调试代码,它将进入方法,但它不会显示剩余的任何动画.下面是我的代码

-(void)imageNewsAnimation
{
    [UIView animateWithDuration:0.35f animations:^{
        _imgNews.animationImages=arrnews;
        _imgNews.animationDuration=1;
        _imgNews.animationRepeatCount=1;
        _imgNews.image = [_imgNews.animationImages lastObject];
        [_imgNews startAnimating];

    } completion:^(BOOL finished) {
        [_imgNews stopAnimating];
        [self imageEventAnimation];
    } ];
}

-(void)imageEventAnimation
{
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        _imgEvents.animationImages=arrEvents;
        _imgEvents.animationDuration=1;
        _imgEvents.animationRepeatCount=1;
        _imgEvents.image = [_imgEvents.animationImages lastObject];
        [_imgEvents startAnimating];


    } completion:^(BOOL finished) {
        [self imageDirectoryAnimation];
    } ];

}
-(void)imageDirectoryAnimation
{
    [UIView animateWithDuration:0.35f animations:^{
        _imgDirectory.animationImages=arrdir;
        _imgDirectory.animationDuration=1;
        _imgDirectory.animationRepeatCount=1;
        _imgDirectory.image = [_imgDirectory.animationImages lastObject];
        [_imgDirectory startAnimating];
    } completion:^(BOOL finished) {
        [self imageInfoAnimation];
    } ];

}

-(void)imageInfoAnimation
{
    [UIView animateWithDuration:0.35f animations:^{
        _imgInfo.animationImages=arrinfo;
        _imgInfo.animationDuration=1;
        _imgInfo.animationRepeatCount=1;
        _imgInfo.image = [_imgInfo.animationImages lastObject];
        [_imgInfo startAnimating];
    } completion:^(BOOL finished) {
        [self imageDirectoryAnimation];
    } ];

}

Below is the Screenshot of imageview place one after another



Best Answer-推荐答案


由于 startAnimating 没有完成处理程序,我会设置如下:

CycleImageView.h

#import <UIKit/UIKit.h>

typedef void (^CycleCompletion)(BOOL finished);

@interface CycleImageView : UIImageView

- (void)startAnimatingWithImagesNSArray<UIImage *>*)images completionCycleCompletion)completion;

@end

CycleImageView.m

#import "CycleImageView.h"

@interface CycleImageView()
@property (nonatomic, copy) CycleCompletion completion;
@end

@implementation CycleImageView

- (void)startAnimatingWithImagesNSArray<UIImage *>*)images completionCycleCompletion)completion {
    self.completion = completion;

    NSMutableArray *imageRefs = [NSMutableArray array];
    for (NSInteger i = 0; i < images.count; i++) {
        [imageRefs addObjectid)images[i].CGImage];
    }

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath"contents"];
    animation.delegate = self;
    animation.calculationMode = kCAAnimationDiscrete;
    animation.duration = 1;
    animation.values = imageRefs;
    animation.repeatCount = 1;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    [self.layer addAnimation:animation forKey"animation"];
}

- (void)animationDidStopCAAnimation *)anim finishedBOOL)flag {
    if (self.completion != nil) {
        self.completion(flag);
        self.completion = nil;
    }
}

@end

要使用它:

将 imgNews、imgEvents、imgDirectory 和 imgInfo 设为 CycleImageView 的所有子类。然后,使用您提供的方法和变量,实现将变为:

- (void)imageNewsAnimation {
    [_imgNews startAnimatingWithImages:arrnews completion:^(BOOL finished) {
       if (finished) {
           [self imageEventAnimation];
       } 
    }];
}

- (void)imageEventAnimation {
    [_imgEvent startAnimatingWithImages:arrEvents completion:^(BOOL finished) {
       if (finished) {
           [self imageDirectoryAnimation];
       } 
    }];
}

- (void)imageDirectoryAnimation {
    [_imgDirectory startAnimatingWithImages:arrdir completion:^(BOOL finished) {
       if (finished) {
           [self imageInfoAnimation];
       } 
    }];
}

- (void)imageInfoAnimation {
    [_imgInfo startAnimatingWithImages:arrinfo completion:^(BOOL finished) {
       if (finished) {
           // NOTE: Are you intentionally creating this indefinite animation loop?
           [self imageDirectoryAnimation];
       } 
    }];
}

编辑:

创建了 gist CycleImageView 也是如此。

关于ios - 完成 objective-c 中的一项后的背靠背动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37020573/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4