程序员

视图播放一组png图片并获得开始结束状态

2017-03-27  本文已影响0人  许佩利翁
在用UIImageView播放一组png图片时发现UIimageView提供的方法并没有动画开始和结束的状态,觉得很不方便,研究了一下写了一个类来解决,菜鸟一枚希望大家别喷...
@interface CXImagesAnimationManage : NSObject

//初始化
+ (nullable instancetype)manager;

/**
 播放一组图片,并获得播放状态

 @param t 执行时间 默认2s
 @param count 重复次数 默认1s
 @param images 图片组
 @param view 播放图片的视图
 @param start 开始
 @param stop 停止
 */
- (void)startAnimationWithDuration:(NSTimeInterval)t
                   withRepeatCount:(NSInteger)count
                        withImages:(nonnull NSArray<UIImage*> *)images
                 withAnimationView:(nonnull UIView *)view
                    withStartBlock:(nullable void(^)(void))start
                     withStopBlock:(nullable void(^)(void))stop;

static NSString *kKeyPath = @"contents";
static NSTimeInterval defaultTime = 1; //默认时间
static NSTimeInterval defaultCount = 1; //默认执行次数
typedef void(^AnimationBlock)();

@interface CXImagesAnimationManage() <CAAnimationDelegate>

@property (nonatomic, copy) AnimationBlock startBlock;
@property (nonatomic, copy) AnimationBlock stopBlock;
@property (nonatomic, copy) CAKeyframeAnimation *anim;

@end

@implementation CXImagesAnimationManage

+ (instancetype)manager {
    static CXImagesAnimationManage *instance = nil;
    static dispatch_once_t t;
    dispatch_once(&t, ^{
        instance = [[CXImagesAnimationManage alloc] init];
    });
    return instance;
}

- (void)startAnimationWithDuration:(NSTimeInterval)t
                   withRepeatCount:(NSInteger)count
                        withImages:(NSArray<UIImage *> *)images
                 withAnimationView:(UIView *)view
                    withStartBlock:(void (^)(void))start
                     withStopBlock:(void (^)(void))stop {
    
    NSMutableArray *imagesArr = [NSMutableArray array];
    
    for (UIImage *img in images) {
        [imagesArr addObject:(__bridge UIImage *)img.CGImage];
    }    
    
    _startBlock = start;
    _stopBlock = stop;
    
    //设置动画属性
    self.anim.duration = t;
    self.anim.values = imagesArr;
    self.anim.repeatCount = count;
    
    [view.layer addAnimation:self.anim forKey:kKeyPath];
}

- (CAKeyframeAnimation *)anim {
    if (!_anim) {
        _anim = [CAKeyframeAnimation animation];
        _anim.keyPath = kKeyPath;
        _anim.duration = defaultTime;
        _anim.delegate = self;
        _anim.repeatCount = defaultCount;
    }
    return _anim;
}

#pragma mark CAAnimtion Delegate Mothod
- (void)animationDidStart:(CAAnimation *)anim {
    if (_startBlock != nil && _startBlock != NULL) {
        _startBlock();
    }
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    //动画结束重置属性
    _anim.duration = defaultTime;
    _anim.repeatCount = defaultCount;
    _anim.values = nil;
    
    if (_stopBlock != nil && _stopBlock != NULL) {
        _stopBlock();
    }
}


    [[CXImagesAnimationManage manager] startAnimationWithDuration:0.75 withRepeatCount:1 withImages:_imagesArr withAnimationView:_imageView withStartBlock:^{
        NSLog(@"动画开始");
    } withStopBlock:^{
        NSLog(@"动画结束");
    }];
上一篇下一篇

猜你喜欢

热点阅读