在流媒體播放器中,用戶往往希望能像主流播放器那樣,透過在畫面上滑動來調節音量、亮度與播放進度。本文將示範如何自定義一個透明 UIButton 來捕捉滑動手勢,並透過代理把事件回傳給頁面。
我們需要在視頻上疊加一個透明的 button 來捕捉並響應用戶的點擊與滑動事件,同時在 .h 中聲明一個代理,把自定義 button 的響應事件傳遞給相應頁面。程式碼如下:
.h 文件
#import <UIKit/UIKit.h> @protocol WBYButtonDelegate <NSObject> /** * 開始觸摸 */ - (void)touchesBeganWithPoint:(CGPoint)point; /** * 結束觸摸 */ - (void)touchesEndWithPoint:(CGPoint)point; /** * 移動手指 */ - (void)touchesMoveWithPoint:(CGPoint)point; @end @interface WBYButton : UIButton /** * 傳遞點擊事件的代理 */ @property (weak, nonatomic) id <WBYButtonDelegate> touchDelegate; @end
.m 文件
#import "WBYButton.h"
@implementation WBYButton
// 觸摸開始
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
// 獲取觸摸開始的坐標
UITouch *touch = [touches anyObject];
CGPoint currentP = [touch locationInView:self];
[self.touchDelegate touchesBeganWithPoint:currentP];
}
// 觸摸結束
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint currentP = [touch locationInView:self];
[self.touchDelegate touchesEndWithPoint:currentP];
}
// 移動
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentP = [touch locationInView:self];
[self.touchDelegate touchesMoveWithPoint:currentP];
}
@end
之所以選擇 UIButton,有兩點原因:一是無需手動開啟 userInteractionEnabled 用戶交互;二是可以很方便地為 UIButton 添加 Target,不必像 UIView 那樣再定義一個 UITapGestureRecognizer。此外,為 UIButton 設置各種狀態的背景顏色與背景圖,也要比 UIView 方便得多。
將自定義 Button 添加到視頻上
// 添加自定義的 Button 到視頻畫面上 self.button = [[WBYButton alloc] initWithFrame:playerLayer.frame]; self.button.touchDelegate = self; [playerView addSubview:self.button];
在代理方法中改變音量、亮度和進度
首先定義一枚舉表示上下左右,這裡只需判斷手指是上下滑動還是左右滑動。
typedef NS_ENUM(NSUInteger, Direction) {
DirectionLeftOrRight,
DirectionUpOrDown,
DirectionNone
};
同時聲明一個表示方向的變量、一個記錄用戶觸摸視頻時坐標的變量、一個記錄觸摸時亮度和音量大小的變量,以及一個記錄播放進度的變量。
@property (assign, nonatomic) Direction direction; @property (assign, nonatomic) CGPoint startPoint; @property (assign, nonatomic) CGFloat startVB; @property (assign, nonatomic) CGFloat startVideoRate;
剛開始觸摸視頻的代理
當用戶首次觸摸視頻時,記錄首次觸摸的坐標、當前音量或亮度、當前播放進度。為了取得當前音量,需先定義 MPVolumeView 與 UISlider。
@property (strong, nonatomic) MPVolumeView *volumeView;// 控制音量的 view @property (strong, nonatomic) UISlider *volumeViewSlider;// 控制音量 // 設置 self.volumeView 的 frame self.volumeView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width * 9.0 / 16.0);
在 getter 方法中初始化:
- (MPVolumeView *)volumeView {
if (_volumeView == nil) {
_volumeView = [[MPVolumeView alloc] init];
[_volumeView sizeToFit];
for (UIView *view in [_volumeView subviews]){
if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
self.volumeViewSlider = (UISlider*)view;
break;
}
}
}
return _volumeView;
}
然後在開始觸摸的代理裡記錄
#pragma mark - 開始觸摸
- (void)touchesBeganWithPoint:(CGPoint)point {
// 記錄首次觸摸坐標
self.startPoint = point;
// 檢測用戶觸摸屏幕的左邊還是右邊,以此判斷要調節音量還是亮度:左邊是亮度,右邊是音量
if (self.startPoint.x <= self.button.frame.size.width / 2.0) {
// 亮度
self.startVB = [UIScreen mainScreen].brightness;
} else {
// 音量
self.startVB = self.volumeViewSlider.value;
}
CMTime ctime = self.avPlayer.currentTime;
self.startVideoRate = ctime.value / ctime.timescale / self.total;
}
接著在拖動的代理方法裡改變音量、亮度和進度
#pragma mark - 拖動
- (void)touchesMoveWithPoint:(CGPoint)point {
// 得出手指在 Button 上移動的距離
CGPoint panPoint = CGPointMake(point.x - self.startPoint.x, point.y - self.startPoint.y);
// 分析出用戶滑動的方向
if (self.direction == DirectionNone) {
if (panPoint.x >= 30 || panPoint.x <= -30) {
// 進度
self.direction = DirectionLeftOrRight;
} else if (panPoint.y >= 30 || panPoint.y <= -30) {
// 音量和亮度
self.direction = DirectionUpOrDown;
}
}
if (self.direction == DirectionNone) {
return;
} else if (self.direction == DirectionUpOrDown) {
// 音量和亮度
if (self.startPoint.x <= self.button.frame.size.width / 2.0) {
// 調節亮度
if (panPoint.y < 0) {
// 增加亮度
[[UIScreen mainScreen] setBrightness:self.startVB + (-panPoint.y / 30.0 / 10)];
} else {
// 減少亮度
[[UIScreen mainScreen] setBrightness:self.startVB - (panPoint.y / 30.0 / 10)];
}
} else {
// 音量
if (panPoint.y < 0) {
// 增大音量
[self.volumeViewSlider setValue:self.startVB + (-panPoint.y / 30.0 / 10) animated:YES];
if (self.startVB + (-panPoint.y / 30 / 10) - self.volumeViewSlider.value >= 0.1) {
[self.volumeViewSlider setValue:0.1 animated:NO];
[self.volumeViewSlider setValue:self.startVB + (-panPoint.y / 30.0 / 10) animated:YES];
}
} else {
// 減少音量
[self.volumeViewSlider setValue:self.startVB - (panPoint.y / 30.0 / 10) animated:YES];
}
}
} else if (self.direction == DirectionLeftOrRight ) {
// 進度
CGFloat rate = self.startVideoRate + (panPoint.x / 30.0 / 20.0);
if (rate > 1) {
rate = 1;
} else if (rate < 0) {
rate = 0;
}
}
}
注意事項:1. 前面提到增大音量的一個 bug,此處的解法是——若偵測到用戶設置的音量比系統當前音量大於 0.1,表示此時設置已無效,於是先將音量設為 0.1,再設回原本無效的值即可,具體做法參考程式碼;2. 修改播放進度時,最好不要在移動方法中實時修改,一方面會造成卡頓,另一方面也沒必要,所以這裡只記錄用戶想調整的進度,等到觸摸結束時再統一設置;3. 改變音量會調用系統自帶的 UI 顯示音量大小,但改變亮度時系統並未提供相應 UI,需要自行添加,夥伴們可按專案需求自行設計。
觸摸結束的代理
#pragma mark - 結束觸摸
- (void)touchesEndWithPoint:(CGPoint)point {
if (self.direction == DirectionLeftOrRight) {
[self.avPlayer seekToTime:CMTimeMakeWithSeconds(self.total * self.currentRate, 1) completionHandler:^(BOOL finished) {
// 在這裡處理進度設置成功後的事情
}];
}
}