Skip to content

Commit

Permalink
注册手势驱动API调整--智能识别手势方向
Browse files Browse the repository at this point in the history
  • Loading branch information
ChavezChen committed Jan 5, 2018
1 parent 0b8b8b4 commit 26672a0
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 196 deletions.
Binary file modified .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions CWLateralSlide.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "CWLateralSlide"
s.version = "1.3.0"
s.version = "1.4.0"
s.summary = "侧滑框架."

s.description = "一行代码集成侧滑功能."
Expand Down Expand Up @@ -74,7 +74,7 @@ Pod::Spec.new do |s|
# Supports git, hg, bzr, svn and HTTP.
#

s.source = { :git => "https://github.com/ChavezChen/CWLateralSlide.git", :tag => "1.3.0" }
s.source = { :git => "https://github.com/ChavezChen/CWLateralSlide.git", :tag => "1.4.0" }


# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down
2 changes: 1 addition & 1 deletion CWLateralSlide/CWDrawerTransition.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ typedef NS_ENUM(NSUInteger,CWDrawerAnimationType) {
@property (nonatomic,copy) NSArray *toViewSubViews;

+ (instancetype)shareInstance;
+ (void)releaseInstance;

+ (void)releaseInstance;

@end

Expand Down
6 changes: 3 additions & 3 deletions CWLateralSlide/CWInteractiveTransition.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@

@interface CWInteractiveTransition : UIPercentDrivenInteractiveTransition

@property (nonatomic,assign)BOOL interacting;

@property (nonatomic,weak) CWLateralSlideConfiguration *configuration;
@property (nonatomic,assign) BOOL interacting;


- (instancetype)initWithTransitiontype:(CWDrawerTransitiontype)type;

+ (instancetype)interactiveWithTransitiontype:(CWDrawerTransitiontype)type;

- (void)addPanGestureForViewController:(UIViewController *)viewController;


@end


Expand Down
71 changes: 31 additions & 40 deletions CWLateralSlide/CWInteractiveTransition.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@
@interface CWInteractiveTransition ()

@property (nonatomic,weak) UIViewController *weakVC;

@property (nonatomic,assign) CWDrawerTransitiontype type;

@property (nonatomic,assign) BOOL openEdgeGesture;

@property (nonatomic,assign) CWDrawerTransitionDirection direction;

@property (nonatomic,strong) CADisplayLink *link;

@property (nonatomic,copy) void(^transitionBlock)();
@property (nonatomic,copy) void(^transitionDirectionAutoBlock)(CWDrawerTransitionDirection direction);

@end

Expand Down Expand Up @@ -59,7 +54,6 @@ - (void)addPanGestureForViewController:(UIViewController *)viewController {

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(cw_handleShowPan:)];
[viewController.view addGestureRecognizer:pan];

}

#pragma mark -GestureRecognizer
Expand All @@ -73,39 +67,37 @@ - (void)cw_handleHiddenPan:(NSNotification *)note {

UIPanGestureRecognizer *pan = note.object;
[self handleGesture:pan];

}

- (void)cw_handleShowPan:(UIPanGestureRecognizer *)pan {

if (_type == CWDrawerTransitiontypeHidden) return;

[self handleGesture:pan];

}

- (void)hiddenBeganTranslationX:(CGFloat)x {
if ((x > 0 && _direction == CWDrawerTransitionDirectionLeft ) || (x < 0 && _direction == CWDrawerTransitionDirectionRight )) return;
if ((x > 0 && _direction == CWDrawerTransitionDirectionLeft ) ||
(x < 0 && _direction == CWDrawerTransitionDirectionRight )) return;
self.interacting = YES;
// NSLog(@"-------------------------");

[self.weakVC dismissViewControllerAnimated:YES completion:nil];
// [self updateInteractiveTransition:0.001];

// NSLog(@"=========================");
}

- (void)showBeganTranslationX:(CGFloat)x gesture:(UIPanGestureRecognizer *)pan {
if ((x < 0 && _direction == CWDrawerTransitionDirectionLeft) || (x > 0 && _direction == CWDrawerTransitionDirectionRight)) return;
// NSLog(@"---->%f", x);
if (x >= 0) _direction = CWDrawerTransitionDirectionLeft;
else _direction = CWDrawerTransitionDirectionRight;

if ((x < 0 && _direction == CWDrawerTransitionDirectionLeft) ||
(x > 0 && _direction == CWDrawerTransitionDirectionRight)) return;

CGFloat locX = [pan locationInView:_weakVC.view].x;
// NSLog(@"locX: %f",locX);
if (_openEdgeGesture && ((locX > 50 && _direction == CWDrawerTransitionDirectionLeft) || (locX < CGRectGetWidth(_weakVC.view.frame) - 50 && _direction == CWDrawerTransitionDirectionRight))) {
return;
}
if (_openEdgeGesture && ((locX > 50 && _direction == CWDrawerTransitionDirectionLeft) || (locX < CGRectGetWidth(_weakVC.view.frame) - 50 && _direction == CWDrawerTransitionDirectionRight))) return;

self.interacting = YES;
if (_transitionBlock) {
_transitionBlock();
if (_transitionDirectionAutoBlock) {
_transitionDirectionAutoBlock(_direction);
}
}

Expand All @@ -114,50 +106,49 @@ - (void)handleGesture:(UIPanGestureRecognizer *)pan {
CGFloat x = [pan translationInView:pan.view].x;

_percent = 0;
// _percent = x / self.configuration.distance;
//_percent = x / self.configuration.distance;
_percent = x / pan.view.frame.size.width;

if ((_direction == CWDrawerTransitionDirectionRight && _type == CWDrawerTransitiontypeShow) || (_direction == CWDrawerTransitionDirectionLeft && _type == CWDrawerTransitiontypeHidden)) {
_percent = -_percent;
}

switch (pan.state) {
case UIGestureRecognizerStateBegan: {
if (_type == CWDrawerTransitiontypeShow) {
[self showBeganTranslationX:x gesture:pan];
}else {
[self hiddenBeganTranslationX:x];
}
case UIGestureRecognizerStateBegan:
break;
}
case UIGestureRecognizerStateChanged: {
_percent = fminf(fmaxf(_percent, 0.001), 1.0);
// NSLog(@"=x:%f width:%f ==%f",x,self.configuration.distance,_percent);
[self updateInteractiveTransition:_percent];
if (!self.interacting) { // 保证present只调用一次
if (_type == CWDrawerTransitiontypeShow) {
// 0是零界点,滑动慢的时候向右边滑动可能会导致x为0然后在接下来的自动判断方向识别为向左滑
if (x != 0) [self showBeganTranslationX:x gesture:pan];
}else {
[self hiddenBeganTranslationX:x];
}
}else {
_percent = fminf(fmaxf(_percent, 0.001), 1.0);
[self updateInteractiveTransition:_percent];
}
break;
}
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateEnded:{
self.interacting = NO;
if (_percent > 0.5) {
[self startDisplayerLink:_percent toFinish:YES];
}else {
[self startDisplayerLink:_percent toFinish:NO];
}
if (_percent > 0.5) [self startDisplayerLink:_percent toFinish:YES];
else [self startDisplayerLink:_percent toFinish:NO];
break;
}
default:
break;
}
}


- (void)startDisplayerLink:(CGFloat)percent toFinish:(BOOL)finish{

_toFinish = finish;
CGFloat remainDuration = finish ? self.duration * (1 - percent) : self.duration * percent;
_remaincount = 60 * remainDuration;
_oncePercent = finish ? (1 - percent) / _remaincount : percent / _remaincount;

[self starDisplayLink];
}

Expand Down Expand Up @@ -191,7 +182,7 @@ - (void)cw_update {
}

- (void)dealloc {
// NSLog(@"%s",__func__);
// NSLog(@"%s",__func__);
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

Expand Down
4 changes: 0 additions & 4 deletions CWLateralSlide/CWLateralSlideConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ typedef NS_ENUM(NSUInteger,CWDrawerTransitionDirection) {
*/
@property (nonatomic,assign)float scaleY;


/**
菜单滑出的方向,默认为从左侧滑出
*/
Expand All @@ -47,15 +46,13 @@ typedef NS_ENUM(NSUInteger,CWDrawerTransitionDirection) {
@property (nonatomic,strong)UIImage *backImage;



/**
默认配置
@return 配置对象本身
*/
+ (instancetype)defaultConfiguration;


/**
创建一个配置对象的实例方法
Expand All @@ -68,7 +65,6 @@ typedef NS_ENUM(NSUInteger,CWDrawerTransitionDirection) {
*/
- (instancetype)initWithDistance:(float)distance maskAlpha:(float)alpha scaleY:(float)scaleY direction:(CWDrawerTransitionDirection)direction backImage:(UIImage *)backImage;


/**
创建一个配置对象的类方法
Expand Down
14 changes: 4 additions & 10 deletions CWLateralSlide/UIViewController+CWLateralSlide.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

@interface UIViewController (CWLateralSlide)



/**
呼出侧滑控制器的方法(主要)
Expand All @@ -25,17 +23,13 @@
animationType:(CWDrawerAnimationType)animationType
configuration:(CWLateralSlideConfiguration *)configuration;


/**
注册手势驱动方法,一般在viewDidLoad调用,调用之后会添加一个支持侧滑的手势到本控制器
注册手势驱动方法,侧滑呼出的方向自动确定,一般在viewDidLoad调用,调用之后会添加一个支持侧滑的手势到本控制器
@param openEdgeGesture 是否开启边缘手势,边缘手势的开始范围为距离边缘50以内
@param direction 侧滑呼出的方向,请与配置参数里面的方向保持一致,配置参数为nil是传左侧呼出
@param transitionBlock 手势过程中执行的操作。传整个点击present的事件即可(看demo的使用)
@param transitionDirectionAutoBlock 手势过程中执行的操作。根据参数direction传整个点击present的事件即可(看demo的使用)
*/
- (void)cw_registerShowIntractiveWithEdgeGesture:(BOOL)openEdgeGesture
direction:(CWDrawerTransitionDirection)direction transitionBlock:(void(^)())transitionBlock;

- (void)cw_registerShowIntractiveWithEdgeGesture:(BOOL)openEdgeGesture transitionDirectionAutoBlock:(void(^)(CWDrawerTransitionDirection direction))transitionDirectionAutoBlock;

/**
自定义的push方法
Expand Down
41 changes: 19 additions & 22 deletions CWLateralSlide/UIViewController+CWLateralSlide.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

@implementation UIViewController (CWLateralSlide)

// 显示抽屉
- (void)cw_showDrawerViewController:(UIViewController *)viewController animationType:(CWDrawerAnimationType)animationType configuration:(CWLateralSlideConfiguration *)configuration {

if (viewController == nil) return;
Expand Down Expand Up @@ -41,6 +42,24 @@ - (void)cw_showDrawerViewController:(UIViewController *)viewController animation

}

// 注册抽屉手势
- (void)cw_registerShowIntractiveWithEdgeGesture:(BOOL)openEdgeGesture transitionDirectionAutoBlock:(void(^)(CWDrawerTransitionDirection direction))transitionDirectionAutoBlock {

CWLateralSlideAnimator *animator = [CWLateralSlideAnimator lateralSlideAnimatorWithConfiguration:nil];
self.transitioningDelegate = animator;

objc_setAssociatedObject(self, &CWLateralSlideAnimatorKey, animator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

CWInteractiveTransition *interactiveShow = [CWInteractiveTransition interactiveWithTransitiontype:CWDrawerTransitiontypeShow];
[interactiveShow addPanGestureForViewController:self];
[interactiveShow setValue:@(openEdgeGesture) forKey:@"openEdgeGesture"];
[interactiveShow setValue:transitionDirectionAutoBlock forKey:@"transitionDirectionAutoBlock"];
[interactiveShow setValue:@(CWDrawerTransitionDirectionLeft) forKey:@"direction"];

[animator setValue:interactiveShow forKey:@"interactiveShow"];
}

// 抽屉内push界面
- (void)cw_pushViewController:(UIViewController *)viewController{

UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
Expand All @@ -57,30 +76,8 @@ - (void)cw_pushViewController:(UIViewController *)viewController{
}

[self dismissViewControllerAnimated:YES completion:nil];

[nav pushViewController:viewController animated:NO];


}


- (void)cw_registerShowIntractiveWithEdgeGesture:(BOOL)openEdgeGesture direction:(CWDrawerTransitionDirection)direction transitionBlock:(void(^)())transitionBlock {

CWLateralSlideAnimator *animator = [CWLateralSlideAnimator lateralSlideAnimatorWithConfiguration:nil];

self.transitioningDelegate = animator;

objc_setAssociatedObject(self, &CWLateralSlideAnimatorKey, animator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

CWInteractiveTransition *interactiveShow = [CWInteractiveTransition interactiveWithTransitiontype:CWDrawerTransitiontypeShow];
[interactiveShow addPanGestureForViewController:self];
[interactiveShow setValue:@(openEdgeGesture) forKey:@"openEdgeGesture"];
[interactiveShow setValue:transitionBlock forKey:@"transitionBlock"];
[interactiveShow setValue:@(direction) forKey:@"direction"];

[animator setValue:interactiveShow forKey:@"interactiveShow"];
}



@end
8 changes: 4 additions & 4 deletions ViewControllerTransition.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@
666188411FDEC7DE0038D644 /* CWLateralSlide */ = {
isa = PBXGroup;
children = (
666188471FDEC7DE0038D644 /* CWInteractiveTransition.h */,
666188421FDEC7DE0038D644 /* CWInteractiveTransition.m */,
666188431FDEC7DE0038D644 /* CWDrawerTransition.h */,
666188491FDEC7DE0038D644 /* CWDrawerTransition.m */,
666188471FDEC7DE0038D644 /* CWInteractiveTransition.h */,
666188421FDEC7DE0038D644 /* CWInteractiveTransition.m */,
666188441FDEC7DE0038D644 /* CWLateralSlideAnimator.h */,
666188481FDEC7DE0038D644 /* CWLateralSlideAnimator.m */,
666188461FDEC7DE0038D644 /* CWLateralSlideConfiguration.h */,
Expand Down Expand Up @@ -378,7 +378,7 @@
DEVELOPMENT_TEAM = ML49CE4G8Y;
INFOPLIST_FILE = ViewControllerTransition/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.km.ViewControllerTransition1111;
PRODUCT_BUNDLE_IDENTIFIER = com.km.ViewControllerTransition11;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
Expand All @@ -390,7 +390,7 @@
DEVELOPMENT_TEAM = ML49CE4G8Y;
INFOPLIST_FILE = ViewControllerTransition/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.km.ViewControllerTransition1111;
PRODUCT_BUNDLE_IDENTIFIER = com.km.ViewControllerTransition11;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand Down
Binary file not shown.
Loading

0 comments on commit 26672a0

Please sign in to comment.