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