視圖播放一組png圖片并獲得開(kāi)始結(jié)束狀態(tài)

在用UIImageView播放一組png圖片時(shí)發(fā)現(xiàn)UIimageView提供的方法并沒(méi)有動(dòng)畫(huà)開(kāi)始和結(jié)束的狀態(tài),覺(jué)得很不方便,研究了一下寫(xiě)了一個(gè)類來(lái)解決,菜鳥(niǎo)一枚希望大家別噴...
  • <strong>這里是.h文件</strong>
@interface CXImagesAnimationManage : NSObject

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

/**
 播放一組圖片,并獲得播放狀態(tài)

 @param t 執(zhí)行時(shí)間 默認(rèn)2s
 @param count 重復(fù)次數(shù) 默認(rèn)1s
 @param images 圖片組
 @param view 播放圖片的視圖
 @param start 開(kāi)始
 @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; //默認(rèn)時(shí)間
static NSTimeInterval defaultCount = 1; //默認(rèn)執(zhí)行次數(shù)
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;
    
    //設(shè)置動(dòng)畫(huà)屬性
    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 {
    //動(dòng)畫(huà)結(jié)束重置屬性
    _anim.duration = defaultTime;
    _anim.repeatCount = defaultCount;
    _anim.values = nil;
    
    if (_stopBlock != nil && _stopBlock != NULL) {
        _stopBlock();
    }
}


  • <strong>這里是調(diào)用方法</strong>
    [[CXImagesAnimationManage manager] startAnimationWithDuration:0.75 withRepeatCount:1 withImages:_imagesArr withAnimationView:_imageView withStartBlock:^{
        NSLog(@"動(dòng)畫(huà)開(kāi)始");
    } withStopBlock:^{
        NSLog(@"動(dòng)畫(huà)結(jié)束");
    }];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,062評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,374評(píng)論 4 61
  • Swift版本點(diǎn)擊這里歡迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh閱讀 26,135評(píng)論 7 249
  • 我玩文字他玩音樂(lè)我們…… 我不會(huì)唱歌他不會(huì)寫(xiě)詩(shī)我們…… 他給我唱歌我給他寫(xiě)詩(shī)我們…… 我在工作他在上學(xué)我們…… 我...
    洪阿柒閱讀 151評(píng)論 0 1
  • 我希望能夠遠(yuǎn)走,逃離我的所知,逃離我的所有。我想出發(fā),去任何地方,不論是村莊或者荒原,只要不是這里就行。我向往的只...
    你安妮了么閱讀 174評(píng)論 0 0

友情鏈接更多精彩內(nèi)容