先看效果圖毙沾,實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)效果
直接上代碼
CountDownView.h
@protocol CountDownViewDelegate <NSObject>
- (void)popAniFinished;
@end
@interface CountDownView : UIView
@property (nonatomic,weak) id<CountDownViewDelegate> delegate;
// 倒計(jì)時(shí)方法
- (void)startAni; // 開始
- (void)pauseAni; // 暫停
- (void)continueAni; // 繼續(xù)
- (void)cancelAni; // 取消
- (void)popAni; // 放大縮小動(dòng)畫
@end
CountDownView.m文件
#pragma mark - 計(jì)時(shí)器相關(guān)
- (void)startAni {
self.countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(startCountDown) userInfo:nil repeats:YES];
}
// 計(jì)時(shí)器方法
- (void)startCountDown {
_currentCount--;
self.countLabel.text = [NSString stringWithFormat:@"%ld",_currentCount];
if (_currentCount == 0) {
[self cancelAni];
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.fillMode = kCAFillModeForwards;
animationGroup.removedOnCompletion = NO;
animationGroup.autoreverses = NO;
animationGroup.delegate = self;
animationGroup.duration = 0.5;
animationGroup.repeatCount = 0;
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
scaleAnimation.toValue = [NSNumber numberWithFloat:10];
//透明度
CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.values = @[@1, @0.9, @0.8, @0.7, @0.6, @0.5, @0.4, @0.3, @0.2, @0.1, @0];
opacityAnimation.keyTimes = @[@0, @0.1, @0.2, @0.3, @0.4, @0.5, @0.6, @0.7, @0.8, @0.9, @1];
animationGroup.animations = @[scaleAnimation, opacityAnimation];
[self.bjView.layer addAnimation:animationGroup forKey:@"plulsing"];
}else {
[self popAni];
}
}
// 暫停
- (void)pauseAni {
[self.countTimer setFireDate:[NSDate distantFuture]];
}
// 繼續(xù)
- (void)continueAni {
[self.countTimer setFireDate:[NSDate date]];
}
// 取消
- (void)cancelAni {
if (self.countTimer && [self.countTimer isValid]) {
[self.countTimer invalidate];
self.countTimer = nil;
}
}
#pragma mark - pop動(dòng)畫
- (void)popAni {
POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
//它會先縮小到(0.5,0.5),然后再去放大到(1.0,1.0)
springAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0.6, 0.6)];
springAnimation.toValue =[NSValue valueWithCGPoint:CGPointMake(1.0, 1.0)];
springAnimation.springBounciness = 20;
//動(dòng)畫結(jié)束之后的回調(diào)方法
[springAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
NSLog(@"ani finished");
}];
[self.bjView.layer pop_addAnimation:springAnimation forKey:@"SpringAnimation"];
}
#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
[self removeFromSuperview];
if ([self.delegate respondsToSelector:@selector(popAniFinished)]) {
[self.delegate popAniFinished];
}
}
此動(dòng)畫效果需要使用Facebook的第三方庫https://github.com/facebook/pop
demo完整地址如下:https://github.com/lwg123/CountDownAnimation