前言
前段時間封裝了一個視頻播放器使用AVPlayer自定義支持全屏的播放器(三)白嘁,經過一段時間的測試,發(fā)現(xiàn)了許多bug膘流,針對以前遺留的問題進行了修復和更新絮缅。
修復bug
主要修復了播放器頁面不支持旋轉引起全屏音量圖標未旋轉,進度條拖拽不靈敏呼股,Masonry引起約束警告耕魄,網絡不好銷毀播放器引起卡頓,工具條自動消失后需要點擊兩次等bug彭谁。
1.旋轉后音量圖標不旋轉bug
開始使用的是旋轉播放器來實現(xiàn)全屏吸奴,實際頁面未旋轉,所以系統(tǒng)音量圖標方向不對缠局,修改后利用頁面旋轉來實現(xiàn)全屏则奥,這樣就不會引起系統(tǒng)音量圖標方向不對,具體如何使頁面支持旋轉狭园,并且不影響其他頁面請看這里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;
}
使用方法
使用cocoapods導入,pod 'CLPlayer'
铭拧。
1.普通頁面
支持通過Push和模態(tài)創(chuàng)建的頁面赃蛛,無論頁面是否支持旋轉都兼容。播放器默認全部頁面都只支持豎屏搀菩,如果使用后需要某個頁面支持其他方向呕臂,需要重寫下面幾個方法。
頁面支持其他方向代碼
#pragma mark -- 需要頁面支持其他方向肪跋,需要重寫這三個方法歧蒋,默認所有頁面只支持豎屏
// 是否支持自動轉屏
- (BOOL)shouldAutorotate {
return YES;
}
// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
// 默認的屏幕方向(當前ViewController必須是通過模態(tài)出來的UIViewController(模態(tài)帶導航的無效)方式展現(xiàn)出來的,才會調用這個方法)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
使用代碼
CLPlayerView *playerView = [[CLPlayerView alloc] initWithFrame:CGRectMake(0, 90, self.view.CLwidth, 300)];
[self.view addSubview:playerView];
// //重復播放州既,默認不播放
// playerView.repeatPlay = YES;
// //當前控制器是否支持旋轉谜洽,當前頁面支持旋轉的時候需要設置,告知播放器
// playerView.isLandscape = YES;
// //設置等比例全屏拉伸吴叶,多余部分會被剪切
// playerView.fillMode = ResizeAspectFill;
// //設置進度條背景顏色
// playerView.progressBackgroundColor = [UIColor purpleColor];
// //設置進度條緩沖顏色
// playerView.progressBufferColor = [UIColor redColor];
// //設置進度條播放完成顏色
// playerView.progressPlayFinishColor = [UIColor greenColor];
// //全屏是否隱藏狀態(tài)欄
// playerView.fullStatusBarHidden = NO;
// //是否靜音阐虚,默認NO
// playerView.mute = YES;
// //轉子顏色
// playerView.strokeColor = [UIColor redColor];
//視頻地址
playerView.url = [NSURL URLWithString:@"http://baobab.wdjcdn.com/14587093851044544c.mp4"];
//播放
[playerView playVideo];
//返回按鈕點擊事件回調
[playerView backButton:^(UIButton *button) {
NSLog(@"返回按鈕被點擊");
//查詢是否是全屏狀態(tài)
NSLog(@"%d",playerView.isFullScreen);
}];
//播放完成回調
[playerView endPlay:^{
//銷毀播放器
// [playerView destroyPlayer];
// playerView = nil;
NSLog(@"播放完成");
}];
2.TableVIew使用代碼
創(chuàng)建方式和普通頁面一樣,在創(chuàng)建的時候記錄播放器所在cell蚌卤,在cell滑出當前TableView的時候對播放器進行銷毀实束。
播放器創(chuàng)建代碼
#pragma mark - 點擊播放代理
- (void)cl_tableViewCellPlayVideoWithCell:(CLTableViewCell *)cell{
//記錄被點擊的Cell
_cell = cell;
//銷毀播放器
[_playerView destroyPlayer];
CLPlayerView *playerView = [[CLPlayerView alloc] initWithFrame:CGRectMake(0, 0, cell.CLwidth, cell.CLheight)];
_playerView = playerView;
[cell.contentView addSubview:_playerView];
// //重復播放,默認不播放
// _playerView.repeatPlay = YES;
// //當前控制器是否支持旋轉逊彭,當前頁面支持旋轉的時候需要設置咸灿,告知播放器
// _playerView.isLandscape = YES;
// //設置等比例全屏拉伸,多余部分會被剪切
// _playerView.fillMode = ResizeAspectFill;
// //設置進度條背景顏色
// _playerView.progressBackgroundColor = [UIColor purpleColor];
// //設置進度條緩沖顏色
// _playerView.progressBufferColor = [UIColor redColor];
// //設置進度條播放完成顏色
// _playerView.progressPlayFinishColor = [UIColor greenColor];
// //全屏是否隱藏狀態(tài)欄
// _playerView.fullStatusBarHidden = NO;
// //轉子顏色
// _playerView.strokeColor = [UIColor redColor];
//視頻地址
_playerView.url = [NSURL URLWithString:cell.model.videoUrl];
//播放
[_playerView playVideo];
//返回按鈕點擊事件回調
[_playerView backButton:^(UIButton *button) {
NSLog(@"返回按鈕被點擊");
}];
//播放完成回調
[_playerView endPlay:^{
//銷毀播放器
[_playerView destroyPlayer];
_playerView = nil;
_cell = nil;
NSLog(@"播放完成");
}];
}
判斷cell是否滑出TableView代碼
//cell離開tableView時調用
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//因為復用诫龙,同一個cell可能會走多次
if ([_cell isEqual:cell]) {
//區(qū)分是否是播放器所在cell,銷毀時將指針置空
[_playerView destroyPlayer];
_cell = nil;
}
}
播放器效果圖
總結
本次主要是修復以前遺留的bug析显,完善了各種情況的Demo,優(yōu)化了體驗签赃,Demo中有詳細的注釋,具體請在github下載CLPlayer 谷异, 如果喜歡,歡迎star锦聊。