Skip to content

Commit

Permalink
增加长按手势
Browse files Browse the repository at this point in the history
  • Loading branch information
renzifeng committed Oct 11, 2022
1 parent 22eb9be commit e5a4db2
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 5 deletions.
4 changes: 4 additions & 0 deletions ZFPlayer/Classes/ControlView/ZFPlayerControlView.m
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ - (void)gesturePinched:(ZFPlayerGestureControl *)gestureControl scale:(float)sca
}
}

- (void)longPressed:(ZFPlayerGestureControl *)gestureControl state:(ZFLongPressGestureRecognizerState)state {

}

/// 准备播放
- (void)videoPlayer:(ZFPlayerController *)videoPlayer prepareToPlay:(NSURL *)assetURL {
[self hideControlViewWithAnimated:NO];
Expand Down
2 changes: 2 additions & 0 deletions ZFPlayer/Classes/Core/ZFLandscapeRotationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ NS_ASSUME_NONNULL_BEGIN
- (void)updateRotateView:(ZFPlayerView *)rotateView
containerView:(UIView *)containerView;

- (UIView *)fullScreenContainerView;

- (BOOL)isSuppprtInterfaceOrientation:(UIInterfaceOrientation)orientation;

+ (ZFInterfaceOrientationMask)supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window;
Expand Down
6 changes: 6 additions & 0 deletions ZFPlayer/Classes/Core/ZFLandscapeRotationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,10 @@ - (UIInterfaceOrientationMask)supportedInterfaceOrientationsForWindow:(nullable
userInfo:nil];
}

- (UIView *)fullScreenContainerView {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:@"You must override %@ in a subclass.", NSStringFromSelector(_cmd)]
userInfo:nil];
}

@end
4 changes: 4 additions & 0 deletions ZFPlayer/Classes/Core/ZFLandscapeRotationManager_iOS15.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ - (void)interfaceOrientation:(UIInterfaceOrientation)orientation completion:(voi
[UIViewController attemptRotationToDeviceOrientation];
}

- (UIView *)fullScreenContainerView {
return self.landscapeViewController.playerSuperview;
}

- (void)rotationBegin {
if (self.window.isHidden) {
self.window.hidden = NO;
Expand Down
5 changes: 5 additions & 0 deletions ZFPlayer/Classes/Core/ZFLandscapeRotationManager_iOS16.m
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ - (void)interfaceOrientation:(UIInterfaceOrientation)orientation completion:(voi
}];
}


- (UIView *)fullScreenContainerView {
return self.landscapeViewController.view;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
if (window == self.window) {
return 1 << self.currentOrientation;
Expand Down
2 changes: 1 addition & 1 deletion ZFPlayer/Classes/Core/ZFOrientationObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ - (ZFLandscapeRotationManager *)landscapeRotationManager {

- (UIView *)fullScreenContainerView {
if (self.fullScreenMode == ZFFullScreenModeLandscape) {
return self.landscapeRotationManager.landscapeViewController.view;
return self.landscapeRotationManager.fullScreenContainerView;
} else if (self.fullScreenMode == ZFFullScreenModePortrait) {
return self.portraitViewController.view;
}
Expand Down
7 changes: 7 additions & 0 deletions ZFPlayer/Classes/Core/ZFPlayerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,13 @@ - (ZFPlayerGestureControl *)gestureControl {
[self.controlView gesturePinched:control scale:scale];
}
};

gestureControl.longPressed = ^(ZFPlayerGestureControl * _Nonnull control, ZFLongPressGestureRecognizerState state) {
@zf_strongify(self)
if ([self.controlView respondsToSelector:@selector(longPressed:state:)]) {
[self.controlView longPressed:control state:state];
}
};
objc_setAssociatedObject(self, _cmd, gestureControl, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return gestureControl;
Expand Down
18 changes: 16 additions & 2 deletions ZFPlayer/Classes/Core/ZFPlayerGestureControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ typedef NS_OPTIONS(NSUInteger, ZFPlayerDisableGestureTypes) {
ZFPlayerDisableGestureTypesDoubleTap = 1 << 1,
ZFPlayerDisableGestureTypesPan = 1 << 2,
ZFPlayerDisableGestureTypesPinch = 1 << 3,
ZFPlayerDisableGestureTypesAll = (ZFPlayerDisableGestureTypesSingleTap | ZFPlayerDisableGestureTypesDoubleTap | ZFPlayerDisableGestureTypesPan | ZFPlayerDisableGestureTypesPinch)
ZFPlayerDisableGestureTypesLongPress = 1 << 4,
ZFPlayerDisableGestureTypesAll = (ZFPlayerDisableGestureTypesSingleTap | ZFPlayerDisableGestureTypesDoubleTap | ZFPlayerDisableGestureTypesPan | ZFPlayerDisableGestureTypesPinch | ZFPlayerDisableGestureTypesLongPress)
};

/// This enumeration lists some of the pan gesture moving direction that the player not support.
Expand All @@ -72,6 +73,13 @@ typedef NS_OPTIONS(NSUInteger, ZFPlayerDisablePanMovingDirection) {
ZFPlayerDisablePanMovingDirectionAll = (ZFPlayerDisablePanMovingDirectionVertical | ZFPlayerDisablePanMovingDirectionHorizontal) /// Disable pan moving all direction.
};

/// Long press gesture state
typedef NS_ENUM(NSUInteger, ZFLongPressGestureRecognizerState) {
ZFLongPressGestureRecognizerStateBegan,
ZFLongPressGestureRecognizerStateChanged,
ZFLongPressGestureRecognizerStateEnded
};

@interface ZFPlayerGestureControl : NSObject

/// Gesture condition callback.
Expand All @@ -95,6 +103,9 @@ typedef NS_OPTIONS(NSUInteger, ZFPlayerDisablePanMovingDirection) {
/// Pinch gesture callback.
@property (nonatomic, copy, nullable) void(^pinched)(ZFPlayerGestureControl *control, float scale);

/// longpress tap gesture callback.
@property (nonatomic, copy, nullable) void(^longPressed)(ZFPlayerGestureControl *control, ZFLongPressGestureRecognizerState state);

/// The single tap gesture.
@property (nonatomic, strong, readonly) UITapGestureRecognizer *singleTap;

Expand All @@ -104,9 +115,12 @@ typedef NS_OPTIONS(NSUInteger, ZFPlayerDisablePanMovingDirection) {
/// The pan tap gesture.
@property (nonatomic, strong, readonly) UIPanGestureRecognizer *panGR;

/// The pinch tap gesture.
/// The pinch gesture.
@property (nonatomic, strong, readonly) UIPinchGestureRecognizer *pinchGR;

/// The long press gesture.
@property (nonatomic, strong, readonly) UILongPressGestureRecognizer *longPressGR;

/// The pan gesture direction.
@property (nonatomic, readonly) ZFPanDirection panDirection;

Expand Down
41 changes: 39 additions & 2 deletions ZFPlayer/Classes/Core/ZFPlayerGestureControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ @interface ZFPlayerGestureControl ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong) UITapGestureRecognizer *doubleTap;
@property (nonatomic, strong) UIPanGestureRecognizer *panGR;
@property (nonatomic, strong) UIPinchGestureRecognizer *pinchGR;
@property (nonatomic, strong) UILongPressGestureRecognizer *longPressGR;
@property (nonatomic) ZFPanDirection panDirection;
@property (nonatomic) ZFPanLocation panLocation;
@property (nonatomic) ZFPanMovingDirection panMovingDirection;
Expand All @@ -48,13 +49,15 @@ - (void)addGestureToView:(UIView *)view {
[self.targetView addGestureRecognizer:self.doubleTap];
[self.targetView addGestureRecognizer:self.panGR];
[self.targetView addGestureRecognizer:self.pinchGR];
[self.targetView addGestureRecognizer:self.longPressGR];
}

- (void)removeGestureToView:(UIView *)view {
[view removeGestureRecognizer:self.singleTap];
[view removeGestureRecognizer:self.doubleTap];
[view removeGestureRecognizer:self.panGR];
[view removeGestureRecognizer:self.pinchGR];
[view removeGestureRecognizer:self.longPressGR];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
Expand Down Expand Up @@ -110,6 +113,11 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceive
}
}
break;
case ZFPlayerDisableGestureTypesLongPress: {
if (self.disableTypes & ZFPlayerDisableGestureTypesLongPress) {
return NO;
}
}
}

if (self.triggerCondition) return self.triggerCondition(self, type, gestureRecognizer, touch);
Expand Down Expand Up @@ -145,7 +153,7 @@ - (UITapGestureRecognizer *)singleTap {
_singleTap.delegate = self;
_singleTap.delaysTouchesBegan = YES;
_singleTap.delaysTouchesEnded = YES;
_singleTap.numberOfTouchesRequired = 1;
_singleTap.numberOfTouchesRequired = 1;
_singleTap.numberOfTapsRequired = 1;
}
return _singleTap;
Expand All @@ -157,7 +165,7 @@ - (UITapGestureRecognizer *)doubleTap {
_doubleTap.delegate = self;
_doubleTap.delaysTouchesBegan = YES;
_doubleTap.delaysTouchesEnded = YES;
_doubleTap.numberOfTouchesRequired = 1;
_doubleTap.numberOfTouchesRequired = 1;
_doubleTap.numberOfTapsRequired = 2;
}
return _doubleTap;
Expand All @@ -184,6 +192,15 @@ - (UIPinchGestureRecognizer *)pinchGR {
return _pinchGR;
}

- (UILongPressGestureRecognizer *)longPressGR {
if (!_longPressGR) {
_longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
_longPressGR.delegate = self;
_longPressGR.delaysTouchesBegan = YES;
}
return _longPressGR;
}

- (void)handleSingleTap:(UITapGestureRecognizer *)tap {
if (self.singleTapped) self.singleTapped(self);
}
Expand Down Expand Up @@ -258,4 +275,24 @@ - (void)handlePinch:(UIPinchGestureRecognizer *)pinch {
}
}

- (void)handleLongPress:(UILongPressGestureRecognizer *)longPress {
switch (longPress.state) {
case UIGestureRecognizerStateBegan: {
if (self.longPressed) self.longPressed(self, ZFLongPressGestureRecognizerStateBegan);
}
break;
case UIGestureRecognizerStateChanged: {
if (self.longPressed) self.longPressed(self, ZFLongPressGestureRecognizerStateChanged);
}
break;
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateFailed: {
if (self.longPressed) self.longPressed(self, ZFLongPressGestureRecognizerStateEnded);
}
break;
default: break;
}
}

@end
5 changes: 5 additions & 0 deletions ZFPlayer/Classes/Core/ZFPlayerMediaControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ NS_ASSUME_NONNULL_BEGIN
- (void)gesturePinched:(ZFPlayerGestureControl *)gestureControl
scale:(float)scale;

/**
When the gesture longPress changed
*/
- (void)longPressed:(ZFPlayerGestureControl *)gestureControl state:(ZFLongPressGestureRecognizerState)state;

#pragma mark - scrollview

/**
Expand Down

0 comments on commit e5a4db2

Please sign in to comment.