右滑返回上一个界面FDFullscreenPopGesture
来源:互联网 发布:徐水网络互动平台 编辑:程序博客网 时间:2024/05/07 17:37
// The MIT License (MIT)
//
// Copyright (c) 2015-2016 forkingdog ( https://github.com/forkingdog )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#import <UIKit/UIKit.h>
/// "UINavigation+FDFullscreenPopGesture" extends UINavigationController's swipe-
/// to-pop behavior in iOS 7+ by supporting fullscreen pan gesture. Instead of
/// screen edge, you can now swipe from any place on the screen and the onboard
/// interactive pop transition works seamlessly.
///
/// Adding the implementation file of this category to your target will
/// automatically patch UINavigationController with this feature.
@interface UINavigationController (FDFullscreenPopGesture)
/// The gesture recognizer that actually handles interactive pop.
@property (nonatomic,strong, readonly)UIPanGestureRecognizer *fd_fullscreenPopGestureRecognizer;
/// A view controller is able to control navigation bar's appearance by itself,
/// rather than a global way, checking "fd_prefersNavigationBarHidden" property.
/// Default to YES, disable it if you don't want so.
@property (nonatomic,assign) BOOL fd_viewControllerBasedNavigationBarAppearanceEnabled;
@end
/// Allows any view controller to disable interactive pop gesture, which might
/// be necessary when the view controller itself handles pan gesture in some
/// cases.
@interface UIViewController (FDFullscreenPopGesture)
/// Whether the interactive pop gesture is disabled when contained in a navigation
/// stack.
@property (nonatomic,assign) BOOL fd_interactivePopDisabled;
/// Indicate this view controller prefers its navigation bar hidden or not,
/// checked when view controller based navigation bar's appearance is enabled.
/// Default to NO, bars are more likely to show.
@property (nonatomic,assign) BOOL fd_prefersNavigationBarHidden;
/// Max allowed initial distance to left edge when you begin the interactive pop
/// gesture. 0 by default, which means it will ignore this limit.
@property (nonatomic,assign) CGFloat fd_interactivePopMaxAllowedInitialDistanceToLeftEdge;
@end
// The MIT License (MIT)
//
// Copyright (c) 2015-2016 forkingdog ( https://github.com/forkingdog )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#import "UINavigationController+FDFullscreenPopGesture.h"
#import <objc/runtime.h>
@interface _FDFullscreenPopGestureRecognizerDelegate :NSObject <UIGestureRecognizerDelegate>
@property (nonatomic,weak) UINavigationController *navigationController;
@end
@implementation _FDFullscreenPopGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
// Ignore when no view controller is pushed into the navigation stack.
if (self.navigationController.viewControllers.count <= 1) {
returnNO;
}
// Ignore when the active view controller doesn't allow interactive pop.
UIViewController *topViewController =self.navigationController.viewControllers.lastObject;
if (topViewController.fd_interactivePopDisabled) {
returnNO;
}
// Ignore when the beginning location is beyond max allowed initial distance to left edge.
CGPoint beginningLocation = [gestureRecognizerlocationInView:gestureRecognizer.view];
CGFloat maxAllowedInitialDistance = topViewController.fd_interactivePopMaxAllowedInitialDistanceToLeftEdge;
if (maxAllowedInitialDistance >0 && beginningLocation.x > maxAllowedInitialDistance) {
returnNO;
}
// Ignore pan gesture when the navigation controller is currently in transition.
if ([[self.navigationControllervalueForKey:@"_isTransitioning"]boolValue]) {
returnNO;
}
// Prevent calling the handler when the gesture begins in an opposite direction.
CGPoint translation = [gestureRecognizertranslationInView:gestureRecognizer.view];
if (translation.x <=0) {
returnNO;
}
returnYES;
}
@end
typedef void (^_FDViewControllerWillAppearInjectBlock)(UIViewController *viewController,BOOL animated);
@interface UIViewController (FDFullscreenPopGesturePrivate)
@property (nonatomic,copy) _FDViewControllerWillAppearInjectBlock fd_willAppearInjectBlock;
@end
@implementation UIViewController (FDFullscreenPopGesturePrivate)
+ (void)load
{
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [selfclass];
SEL originalSelector =@selector(viewWillAppear:);
SEL swizzledSelector =@selector(fd_viewWillAppear:);
Method originalMethod =class_getInstanceMethod(class, originalSelector);
Method swizzledMethod =class_getInstanceMethod(class, swizzledSelector);
BOOL success =class_addMethod(class, originalSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod));
if (success) {
class_replaceMethod(class, swizzledSelector,method_getImplementation(originalMethod),method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (void)fd_viewWillAppear:(BOOL)animated
{
// Forward to primary implementation.
[selffd_viewWillAppear:animated];
if (self.fd_willAppearInjectBlock) {
self.fd_willAppearInjectBlock(self, animated);
}
}
- (_FDViewControllerWillAppearInjectBlock)fd_willAppearInjectBlock
{
returnobjc_getAssociatedObject(self,_cmd);
}
- (void)setFd_willAppearInjectBlock:(_FDViewControllerWillAppearInjectBlock)block
{
objc_setAssociatedObject(self,@selector(fd_willAppearInjectBlock), block,OBJC_ASSOCIATION_COPY_NONATOMIC);
}
@end
@implementation UINavigationController (FDFullscreenPopGesture)
+ (void)load
{
// Inject "-pushViewController:animated:"
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [selfclass];
SEL originalSelector =@selector(pushViewController:animated:);
SEL swizzledSelector =@selector(fd_pushViewController:animated:);
Method originalMethod =class_getInstanceMethod(class, originalSelector);
Method swizzledMethod =class_getInstanceMethod(class, swizzledSelector);
BOOL success =class_addMethod(class, originalSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod));
if (success) {
class_replaceMethod(class, swizzledSelector,method_getImplementation(originalMethod),method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (void)fd_pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (![self.interactivePopGestureRecognizer.view.gestureRecognizerscontainsObject:self.fd_fullscreenPopGestureRecognizer]) {
// Add our own gesture recognizer to where the onboard screen edge pan gesture recognizer is attached to.
[self.interactivePopGestureRecognizer.viewaddGestureRecognizer:self.fd_fullscreenPopGestureRecognizer];
// Forward the gesture events to the private handler of the onboard gesture recognizer.
NSArray *internalTargets = [self.interactivePopGestureRecognizervalueForKey:@"targets"];
id internalTarget = [internalTargets.firstObjectvalueForKey:@"target"];
SEL internalAction =NSSelectorFromString(@"handleNavigationTransition:");
self.fd_fullscreenPopGestureRecognizer.delegate = self.fd_popGestureRecognizerDelegate;
[self.fd_fullscreenPopGestureRecognizeraddTarget:internalTargetaction:internalAction];
// Disable the onboard gesture recognizer.
self.interactivePopGestureRecognizer.enabled = NO;
}
// Handle perferred navigation bar appearance.
[selffd_setupViewControllerBasedNavigationBarAppearanceIfNeeded:viewController];
// Forward to primary implementation.
if (![self.viewControllerscontainsObject:viewController]) {
[selffd_pushViewController:viewControlleranimated:animated];
}
}
- (void)fd_setupViewControllerBasedNavigationBarAppearanceIfNeeded:(UIViewController *)appearingViewController
{
if (!self.fd_viewControllerBasedNavigationBarAppearanceEnabled) {
return;
}
__weaktypeof(self) weakSelf =self;
_FDViewControllerWillAppearInjectBlock block = ^(UIViewController *viewController,BOOL animated) {
__strongtypeof(weakSelf) strongSelf = weakSelf;
if (strongSelf) {
[strongSelf setNavigationBarHidden:viewController.fd_prefersNavigationBarHiddenanimated:animated];
}
};
// Setup will appear inject block to appearing view controller.
// Setup disappearing view controller as well, because not every view controller is added into
// stack by pushing, maybe by "-setViewControllers:".
appearingViewController.fd_willAppearInjectBlock = block;
UIViewController *disappearingViewController =self.viewControllers.lastObject;
if (disappearingViewController && !disappearingViewController.fd_willAppearInjectBlock) {
disappearingViewController.fd_willAppearInjectBlock = block;
}
}
- (_FDFullscreenPopGestureRecognizerDelegate *)fd_popGestureRecognizerDelegate
{
_FDFullscreenPopGestureRecognizerDelegate *delegate =objc_getAssociatedObject(self,_cmd);
if (!delegate) {
delegate = [[_FDFullscreenPopGestureRecognizerDelegatealloc] init];
delegate.navigationController =self;
objc_setAssociatedObject(self,_cmd, delegate,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return delegate;
}
- (UIPanGestureRecognizer *)fd_fullscreenPopGestureRecognizer
{
UIPanGestureRecognizer *panGestureRecognizer =objc_getAssociatedObject(self,_cmd);
if (!panGestureRecognizer) {
panGestureRecognizer = [[UIPanGestureRecognizeralloc] init];
panGestureRecognizer.maximumNumberOfTouches =1;
objc_setAssociatedObject(self,_cmd, panGestureRecognizer,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return panGestureRecognizer;
}
- (BOOL)fd_viewControllerBasedNavigationBarAppearanceEnabled
{
NSNumber *number =objc_getAssociatedObject(self,_cmd);
if (number) {
return number.boolValue;
}
self.fd_viewControllerBasedNavigationBarAppearanceEnabled =YES;
returnYES;
}
- (void)setFd_viewControllerBasedNavigationBarAppearanceEnabled:(BOOL)enabled
{
SEL key =@selector(fd_viewControllerBasedNavigationBarAppearanceEnabled);
objc_setAssociatedObject(self, key,@(enabled),OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
@implementation UIViewController (FDFullscreenPopGesture)
- (BOOL)fd_interactivePopDisabled
{
return [objc_getAssociatedObject(self,_cmd) boolValue];
}
- (void)setFd_interactivePopDisabled:(BOOL)disabled
{
objc_setAssociatedObject(self,@selector(fd_interactivePopDisabled),@(disabled),OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)fd_prefersNavigationBarHidden
{
return [objc_getAssociatedObject(self,_cmd) boolValue];
}
- (void)setFd_prefersNavigationBarHidden:(BOOL)hidden
{
objc_setAssociatedObject(self,@selector(fd_prefersNavigationBarHidden),@(hidden),OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGFloat)fd_interactivePopMaxAllowedInitialDistanceToLeftEdge
{
#if CGFLOAT_IS_DOUBLE
return [objc_getAssociatedObject(self,_cmd) doubleValue];
#else
return [objc_getAssociatedObject(self,_cmd) floatValue];
#endif
}
- (void)setFd_interactivePopMaxAllowedInitialDistanceToLeftEdge:(CGFloat)distance
{
SEL key =@selector(fd_interactivePopMaxAllowedInitialDistanceToLeftEdge);
objc_setAssociatedObject(self, key,@(MAX(0, distance)),OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
- 右滑返回上一个界面FDFullscreenPopGesture
- android右滑返回上一个界面
- FDFullscreenPopGesture全局右滑
- Android 返回上一个界面,返回上一个Activity
- 如何返回上一个界面并传值给上一个界面
- 关于右滑返回到上级界面
- 仿ios右滑界面返回功能
- 给界面添加右滑返回
- Android - 返回上一个界面finish()方法
- Android - 返回上一个界面finish()方法
- 滑动返回上一个界面的实现
- Android 中实现在界面上右划返回上一个界面
- Activity多级界面跳转不断返回上一个界面解决方案
- 【iOS】右滑返回上一级界面手势
- 创建全屏右滑返回上一级界面
- NavigationBar隐藏后 返回上一个界面黑框问题解决
- WebView_返回键返回到上一个HTML界面不回到源生界面的方法
- IOS7右滑返回
- Django 模板
- 利用Quart2D作一些简单的图形
- FFMPEG 处理RTMP流媒体的命令大全
- TD udate
- redis日志出现Can't save in background fork Cannot allocate memory
- 右滑返回上一个界面FDFullscreenPopGesture
- Android 软引用(SoftReference)与LruCache
- Java SPI机制
- Android drawable
- NumberPicker简单功能用法
- JavaScript操作Cookie
- 25-ZigZag Conversion
- Java中传值还是传引用
- DB2查询行号