项目介绍:
前言
前段时间封装了一个视频播放器使用AVPlayer自定义支持全屏的播放器(三),经过一段时间的测试,发现了许多bug,针对以前遗留的问题进行了修复和更新。
修复bug
主要修复了播放器页面不支持旋转引起全屏音量图标未旋转,进度条拖拽不灵敏,Masonry引起约束警告,网络不好销毁播放器引起卡顿以及一些小问题。
1.旋转后音量图标不旋转bug
开始使用的是旋转播放器来实现全屏,实际页面未旋转,所以系统音量图标方向不对,修改后利用页面旋转来实现全屏,这样就不会引起系统音量图标方向不对,具体如何使页面支持旋转,并且不影响其他页面请看这里iOS页面旋转详解。
2.进度条拖拽不灵敏
由于自定义了进度条的图标,引起进度条拖拽不灵敏,这里在自定义进度条内部重写- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
,- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
这两个方法,增大响应的范围。
代码
//检查点击事件点击范围是否能够交给self处理
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//调用父类方法,找到能够处理event的view
UIView* result = [super hitTest:point withEvent:event];
if (result != self) {
/*如果这个view不是self,我们给slider扩充一下响应范围,
这里的扩充范围数据就可以自己设置了
*/
if ((point.y >= -15) &&
(point.y < (_lastBounds.size.height + SLIDER_Y_BOUND)) &&
(point.x >= 0 && point.x < CGRectGetWidth(self.bounds))) {
//如果在扩充的范围类,就将event的处理权交给self
result = self;
}
}
//否则,返回能够处理的view
return result;
}
//检查是点击事件的点是否在slider范围内
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
//调用父类判断
BOOL result = [super pointInside:point withEvent:event];
if (!result) {
//同理,如果不在slider范围类,扩充响应范围
if ((point.x >= (_lastBounds.origin.x - SLIDER_X_BOUND)) && (point.x <= (_lastBounds.origin.x + _lastBounds.size.width + SLIDER_X_BOUND))
&& (point.y >= -SLIDER_Y_BOUND) && (point.y < (_lastBounds.size.height + SLIDER_Y_BOUND))) {
//在扩充范围内,返回yes
result = YES;
}
}
//否则返回父类的结果
return result;
}
新增功能
新增加了转子动画,增加拖拽后转子衔接动画,增加各类接口。
转子动画
利用CAShapeLayer
和UIBezierPath
做了一个简单的加载动画。
代码
@interface AILoadingView ()<CAAnimationDelegate>
@property(nonatomic,strong)CAShapeLayer *loadingLayer;
/** 当前的index*/
@property(nonatomic,assign)NSInteger index;
/** 是否能用*/
@property(nonatomic,assign,getter=isEnable)BOOL enable;
@end
@implementation AILoadingView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_index = 0;
_enable = YES;
_duration = 2.;
[self createUI];
}
return self;
}
-(void)layoutSubviews {
[super layoutSubviews];
UIBezierPath *path = [self cycleBezierPathIndex:_index];
self.loadingLayer.path = path.CGPath;
}
- (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 {
self.loadingLayer = [CAShapeLayer layer];
self.loadingLayer.lineWidth = 2.;
self.loadingLayer.fillColor = [UIColor clearColor].CGColor;
self.loadingLayer.strokeColor = [UIColor blackColor].CGColor;
[self.layer addSublayer:self.loadingLayer];
self.loadingLayer.lineCap = kCALineCapRound;
}
- (void)loadingAnimation {
CABasicAnimation *strokeStartAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
strokeStartAnimation.fromValue = @0;
strokeStartAnimation.toValue = @1.;
strokeStartAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
CABasicAnimation *strokeEndAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
strokeEndAnimation.fromValue = @.0;
strokeEndAnimation.toValue = @1.;
strokeEndAnimation.duration = self.duration * 0.5;
CAAnimationGroup *strokeAniamtionGroup = [CAAnimationGroup animation];
strokeAniamtionGroup.duration = self.duration;
strokeAniamtionGroup.delegate = self;
strokeAniamtionGroup.animations = @[strokeEndAnimation,strokeStartAnimation];
[self.loadingLayer addAnimation:strokeAniamtionGroup forKey:@"strokeAniamtionGroup"];
}
#pragma mark -CAAnimationDelegate
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
if (!self.isEnable) {
return;
}
_index++;
self.loadingLayer.path = [self cycleBezierPathIndex:_index %3].CGPath;
[self loadingAnimation];
}
#pragma mark -public
- (void)starAnimation {
if (self.loadingLayer.animationKeys.count > 0) {
return;
}
self.hidden = NO;
self.enable = YES;
[self loadingAnimation];
}
- (void)stopAnimation {
self.hidden = YES;
self.enable = NO;
[self.loadingLayer removeAllAnimations];
}
- (void)setStrokeColor:(UIColor *)strokeColor {
_strokeColor = strokeColor;
self.loadingLayer.strokeColor = strokeColor.CGColor;
}
播放器效果图
总结
本次主要是修复以前遗留的bug,完善了各种情况的Demo,优化了体验,具体请在github下载CLPlayer , 如果喜欢,欢迎star。