• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 完成 objective-c 中的一项后的背靠背动画

[复制链接]
菜鸟教程小白 发表于 2022-12-12 19:43:14 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想在 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap