美文网首页程序员
视图播放一组png图片并获得开始结束状态

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

作者: 许佩利翁 | 来源:发表于2017-03-27 19:02 被阅读0次
    在用UIImageView播放一组png图片时发现UIimageView提供的方法并没有动画开始和结束的状态,觉得很不方便,研究了一下写了一个类来解决,菜鸟一枚希望大家别喷...
    • <strong>这里是.h文件</strong>
    @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;
    
    
    • <strong>这里是.m文件</strong>
    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();
        }
    }
    
    
    
    • <strong>这里是调用方法</strong>
        [[CXImagesAnimationManage manager] startAnimationWithDuration:0.75 withRepeatCount:1 withImages:_imagesArr withAnimationView:_imageView withStartBlock:^{
            NSLog(@"动画开始");
        } withStopBlock:^{
            NSLog(@"动画结束");
        }];
    

    相关文章

      网友评论

        本文标题:视图播放一组png图片并获得开始结束状态

        本文链接:https://www.haomeiwen.com/subject/udorottx.html