Skip to content

Commit

Permalink
修复转子动画未销毁引起卡死bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JmoVxia committed Apr 22, 2018
1 parent e97221d commit 6d74176
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 41 deletions.
2 changes: 1 addition & 1 deletion CLPlayer.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'CLPlayer'
s.version = '1.2.1'
s.version = '1.2.2'
s.summary = 'AVPlayer定制的视频播放器'
s.homepage = 'https://github.com/JmoVxia/CLPlayer'
s.license = 'MIT'
Expand Down
12 changes: 5 additions & 7 deletions CLPlayer/AILoadingView.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@

@interface AILoadingView : UIView

/**
开始动画
*/
/**开始动画*/
- (void)starAnimation;

/**
停止动画
*/
/**停止动画*/
- (void)stopAnimation;
/**销毁动画*/
- (void)destroyAnimation;
/** 一次动画所持续时长 默认2秒*/
@property(nonatomic,assign)NSTimeInterval duration;
/** 线条颜色*/
@property (nonatomic, strong) UIColor *strokeColor;

@end
39 changes: 23 additions & 16 deletions CLPlayer/AILoadingView.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ @interface AILoadingView ()<CAAnimationDelegate>
@end
@implementation AILoadingView

- (instancetype)initWithFrame:(CGRect)frame
{
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
_index = 0;
Expand All @@ -29,17 +28,11 @@ - (instancetype)initWithFrame:(CGRect)frame
}
return self;
}
-(void)layoutSubviews {
[super layoutSubviews];
UIBezierPath *path = [self cycleBezierPathIndex:_index];
self.loadingLayer.path = path.CGPath;
}

- (UIBezierPath*)cycleBezierPathIndex:(NSInteger)index {
- (UIBezierPath*)cycleBezierPathIndex:(NSInteger)index{
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height *0.5) radius:self.bounds.size.width * 0.5 startAngle:index * (M_PI* 2)/3 endAngle:index * (M_PI* 2)/3 + 2*M_PI * 4/3 clockwise:YES];
return path;
}
- (void)createUI {
- (void)createUI{
self.loadingLayer = [CAShapeLayer layer];
self.loadingLayer.lineWidth = 2.;
self.loadingLayer.fillColor = [UIColor clearColor].CGColor;
Expand All @@ -48,7 +41,7 @@ - (void)createUI {
self.loadingLayer.lineCap = kCALineCapRound;
[self loadingAnimation];
}
- (void)loadingAnimation {
- (void)loadingAnimation{
CABasicAnimation *strokeStartAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
strokeStartAnimation.fromValue = @0;
strokeStartAnimation.toValue = @1.;
Expand All @@ -68,7 +61,7 @@ - (void)loadingAnimation {
[self.loadingLayer addAnimation:strokeAniamtionGroup forKey:@"strokeAniamtionGroup"];
}
#pragma mark -CAAnimationDelegate
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
if (self.isHidden) {
_realFinish = YES;
return;
Expand All @@ -79,7 +72,7 @@ -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
}

#pragma mark -public
- (void)starAnimation {
- (void)starAnimation{
if (self.loadingLayer.animationKeys.count > 0) {
return;
}
Expand All @@ -88,15 +81,29 @@ - (void)starAnimation {
[self loadingAnimation];
}
}
- (void)stopAnimation {
- (void)stopAnimation{
self.hidden = YES;
self.index = 0;
[self.loadingLayer removeAllAnimations];
}
- (void)setStrokeColor:(UIColor *)strokeColor {
- (void)setStrokeColor:(UIColor *)strokeColor{
_strokeColor = strokeColor;
self.loadingLayer.strokeColor = strokeColor.CGColor;
}

- (void)destroyAnimation{
[self stopAnimation];
[self.loadingLayer removeFromSuperlayer];
self.loadingLayer = nil;
}
-(void)layoutSubviews{
[super layoutSubviews];
UIBezierPath *path = [self cycleBezierPathIndex:_index];
self.loadingLayer.path = path.CGPath;
}
-(void)dealloc{
#ifdef DEBUG
NSLog(@"转子动画销毁了");
#endif
}

@end
2 changes: 1 addition & 1 deletion CLPlayer/CLPlayerMaskView.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/**底部工具条*/
@property (nonatomic,strong) UIView *bottomToolBar;
/**转子*/
@property (nonatomic,strong) AILoadingView *activity;
@property (nonatomic,strong) AILoadingView *loadingView;
/**顶部工具条返回按钮*/
@property (nonatomic,strong) UIButton *backButton;
/**底部工具条播放按钮*/
Expand Down
20 changes: 10 additions & 10 deletions CLPlayer/CLPlayerMaskView.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ -(instancetype)initWithFrame:(CGRect)frame{
- (void)initViews{
[self addSubview:self.topToolBar];
[self addSubview:self.bottomToolBar];
[self addSubview:self.activity];
[self addSubview:self.loadingView];
[self addSubview:self.failButton];
[self.topToolBar addSubview:self.backButton];
[self.bottomToolBar addSubview:self.playButton];
Expand All @@ -55,7 +55,7 @@ - (void)makeConstraints{
make.height.mas_equalTo(ToolBarHeight);
}];
//转子
[self.activity mas_makeConstraints:^(MASConstraintMaker *make) {
[self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.mas_equalTo(self);
make.size.mas_equalTo(CGSizeMake(40, 40));
}];
Expand Down Expand Up @@ -136,13 +136,13 @@ - (UIView *) bottomToolBar{
return _bottomToolBar;
}
//转子
- (AILoadingView *) activity{
if (_activity == nil){
_activity = [[AILoadingView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
_activity.strokeColor = [UIColor whiteColor];
[_activity starAnimation];
- (AILoadingView *) loadingView{
if (_loadingView == nil){
_loadingView = [[AILoadingView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
_loadingView.strokeColor = [UIColor whiteColor];
[_loadingView starAnimation];
}
return _activity;
return _loadingView;
}
//返回按钮
- (UIButton *) backButton{
Expand Down Expand Up @@ -262,8 +262,8 @@ - (void)fullButtonAction:(UIButton *)button{
//失败按钮
- (void)failButtonAction:(UIButton *)button{
self.failButton.hidden = YES;
self.activity.hidden = NO;
[self.activity starAnimation];
self.loadingView.hidden = NO;
[self.loadingView starAnimation];
if (_delegate && [_delegate respondsToSelector:@selector(cl_failButtonAction:)]) {
[_delegate cl_failButtonAction:button];
}else{
Expand Down
14 changes: 8 additions & 6 deletions CLPlayer/CLPlayerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ -(void)setProgressPlayFinishColor:(UIColor *)progressPlayFinishColor{
#pragma mark - 转子颜色
-(void)setStrokeColor:(UIColor *)strokeColor{
_strokeColor = strokeColor;
self.maskView.activity.strokeColor = strokeColor;
self.maskView.loadingView.strokeColor = strokeColor;
}
#pragma mark - 小屏是否需要手势控制
-(void)setSmallGestureControl:(BOOL)smallGestureControl{
Expand Down Expand Up @@ -272,16 +272,16 @@ - (void)setState:(CLPlayerState)state{
}
_state = state;
if (state == CLPlayerStateBuffering) {
[self.maskView.activity starAnimation];
[self.maskView.loadingView starAnimation];
}else if (state == CLPlayerStateFailed){
[self.maskView.activity stopAnimation];
[self.maskView.loadingView stopAnimation];
self.maskView.failButton.hidden = NO;
self.maskView.playButton.selected = NO;
#ifdef DEBUG
NSLog(@"加载失败");
#endif
}else{
[self.maskView.activity stopAnimation];
[self.maskView.loadingView stopAnimation];
if (_isUserPlay) {
[self playVideo];
}
Expand Down Expand Up @@ -634,7 +634,7 @@ -(void)cl_fullButtonAction:(UIButton *)button{
}
#pragma mark - 播放失败按钮点击事件
-(void)cl_failButtonAction:(UIButton *)button{
[self.maskView.activity starAnimation];
[self.maskView.loadingView starAnimation];
self.maskView.playButton.selected = YES;
[self performSelector:@selector(failButtonResetPlay)
withObject:@"FailButtonResetPlay"
Expand Down Expand Up @@ -745,6 +745,8 @@ - (void)destroyPlayer{
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(failButtonResetPlay)
object:@"FailButtonResetPlay"];
//销毁转子动画
[self.maskView.loadingView destroyAnimation];
//移除
[self.playerLayer removeFromSuperlayer];
[self removeFromSuperview];
Expand Down Expand Up @@ -780,7 +782,7 @@ - (void)resetPlayer{
//重新添加工具条定时消失定时器
self.toolBarDisappearTime = _toolBarDisappearTime;
//开始转子
[self.maskView.activity starAnimation];
[self.maskView.loadingView starAnimation];
}
#pragma mark - 取消定时器
//销毁所有定时器
Expand Down

0 comments on commit 6d74176

Please sign in to comment.