给UIImageView设置一组图片动画,每张图片停留时间不一祥

本文介绍了一个 UIView 的分类,用于实现关键帧动画效果。通过传入图片数组、时间数组及循环次数等参数,可以轻松创建复杂的动画。文章提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一组图片
给UIView 写个分类

//UIView+KeyFrameAnimation.h
#import <UIKit/UIKit.h>
@interface UIView (KeyFrameAnimation)
- (void)addAnimationWithImages:(NSArray *)animationImages
                         times:(NSArray *)times
                   repeatCount:(float)count
                animationFrame:(CGRect)frame;

@end
//  UIView+KeyFrameAnimation.m
#import "UIView+KeyFrameAnimation.h"

@implementation UIView (KeyFrameAnimation)
- (void)addAnimationWithImages:(NSArray *)animationImages times:(NSArray *)times repeatCount:(float)count animationFrame:(CGRect)frame{

    NSArray *sumArray = [self sumWithArray:times];//前几个的和;
    NSTimeInterval total = [[sumArray lastObject] doubleValue];

    NSMutableArray  *keyTimes = [NSMutableArray arrayWithObjects:@0.0f, nil];
    for (NSNumber *time in sumArray) {
        NSTimeInterval timeFloat = [time floatValue] / total;
        [keyTimes addObject:[NSNumber numberWithDouble:timeFloat]];
    }
    [keyTimes addObject:@1.0f];

    //CAImage 数组
    NSMutableArray *array = [NSMutableArray array];
    for (UIImage *image in animationImages) {
        CGImageRef cgImage = [image CGImage];
        [array addObject:(__bridge id _Nonnull)(cgImage)];
    }

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.calculationMode = kCAAnimationDiscrete;
    animation.keyPath = @"contents";
    animation.duration = total;
    animation.delegate = self;
    animation.repeatCount = count;
    animation.values = array;
    animation.keyTimes = keyTimes;//每帧比例相对1的比例时间数组[0...1]

    CALayer *layer = [CALayer layer];
    layer.frame = frame;
    [layer addAnimation:animation forKey:nil];
    [self.layer addSublayer:layer];
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    [[self.layer.sublayers lastObject] removeFromSuperlayer];
}

-(NSArray *)sumWithArray:(NSArray*)array{
    NSMutableArray *mutableArray = [NSMutableArray array];
    double sum = 0;
    for (int i = 0; i < array.count; i++) {
        sum += [((NSNumber *)array[i]) floatValue];
        [mutableArray addObject:[NSNumber numberWithFloat:sum]];
    }
    return mutableArray;
}
@end

调用

-(void)animation{
    NSArray *imagePaths = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"animations"];
    NSMutableArray *animationImages = [NSMutableArray array];
    NSArray* times = @[@0.7, @0.4, @0.3, @0.5, @0.4, @0.4, @0.4, @0.8];
    for (NSString *imageName in imagePaths) {
        [animationImages addObject:[UIImage imageNamed:imageName]];
    }
    [self.imageView addAnimationWithImages:animationImages times:times repeatCount:1 animationFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值