win7如何禁掉数字签名UIScrollView里的UIPanGestureRecognizer

问题描述:横向UIScrollView里面的子控件,我使用了UIPanGestureRecognizer来实现拖动,但是发现UIScrollView无法响应滚动事件,因为scroll里面布满了我放的控件,所以不能响应。如果先让scroll响应
[panGestureRecognizer requireGestureRecognizerToFail:sroll.panGestureRecognizer]//先处理scroll发现,我添加的手势又不响应了,因为sroll滚动每次都成功。
解决办法:后来在 栈溢出 问答平台找到了一个没有采纳的答案解决了问题,使用手势代理,通过
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer来解决
这个方法是用来处理多手势共存的,返回NO则响应一个手势,返回YES为同时响应,我的处理方法是,判断当前gestureRecognizer是否是panGestureRecognizer,然后判断方向,如果为上下方向则返回NO,则scroll不响应,否则都响应,问题完美解决。
当然拖动手势没法直接获取方向需要通过
CGPoint translation = [(UIPanGestureRecognizer*)gestureRecognizer translationInView:self];来获得,如果translation.y的绝对值大于translation.x的绝对值就可以看成是上下方向。
本文已收录于以下专栏:
相关文章推荐
解决UIPanGestureRecognizer和UIScrollView冲突问题
拖拽手势UIPanGestureRecognizer是什么?
拖拽手势顾名思义就是拖拽。苹果的官方文档是这样描述的:
使用有道词典翻译后是这样的:
UIPanGestureRecognizer U...
UIPanGestureRecognizer是UIGestureRecognizer类的一个扩展类,其扩展类有UITapGestureRecognizer,UIPinchGestureRecogniz...
UIPanGestureRecognizer *panGesture = (UIPanGestureRecognizer*)gestureR
pan.delegate...
1、发现的问题
1. 判断是否是横向屏:BOOL b=UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation);
     获取设备u...
对于sysctl有关的函数,网上资料比较少,我整理了一些大家一起学习探讨。
1.sysctl有关函数
sysctl,sysctlname,sysctlnametomib,这三个函数的功能是获取或者...
sysctl 函数 11:24sysctl操作我们对于路由套接口的主要兴趣点在于使用sysctl函数检查路由表和接口清单。使用该函数检查路由表清单不需要超级用户权限。 #inclu...
//可通过苹果review
- (NSString*)getDeviceVersion
    size_
    s...
一、UIScrollView滚动视图
#import &MainViewController.h&
@interface
MainViewController ...
他的最新文章
讲师:王哲涵
讲师:韦玮
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)iOS UIScrollView一个容易忽略的细节
作者:joshualiyz
最近项目中需要用到横向滚动的UIScrollView,另外页面还要支持系统右滑退出的逻辑。说到这里大家肯定都知道了,我们要解决的是一个手势冲突的问题。解决方案网上很多,我说一下自己的解决方法:自定义一个UIScrollView,当然要实现UIGestureRecognizerDelegate
@interface GestureScrollView : UIScrollView&UIGestureRecognizerDelegate&
然后重写- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
@implementation GestureScrollView
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]
[otherGestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]
self.contentOffset.x == 0) {
return YES;
return NO;
到此,问题就解决了。通常,这可能会是一道面试题,通常,问到这里问题就结束了。我喜欢刨根问底,于是会多问一句“有没有漏掉什么?”是啊,总感觉怪怪的,我们只是实现了代理方法,并没有写xxx.delegate = self的逻辑啊。很多面试者会直接说,设置UIScrollView的panGestureRecognizer的delegate指向UIScrollView自己!看下UIScrollView.h:
// Use these accessors to configure the scroll view's built-in gesture recognizers.
// Do not change the gestures' delegates or override the getters for these properties.
@property(nonatomic, readonly) UIPanGestureRecognizer *panGestureRecognizer NS_AVAILABLE_IOS(5_0);
这货是readonly的,还不能改。
看来这个问题没那么简单,想找到正确答案,我们来做个简单实验。添加如下代码:
- (instancetype)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
NSLog(@"self = %@, panGestureRecognizer.delegate = %@", self, self.panGestureRecognizer.delegate);
运行log如下:
self = &GestureScrollView: 0x; baseClass = UIScrollV frame = (0 34.5; 375 568); clipsToBounds = YES; gestureRecognizers = &NSArray: 0x&; layer = &CALayer: 0x14551ee40&; contentOffset: {0, 0}; contentSize: {0, 0}&
panGestureRecognizer.delegate = &GestureScrollView: 0x; baseClass = UIScrollV frame = (0 34.5; 375 568); clipsToBounds = YES; gestureRecognizers = &NSArray: 0x&; layer = &CALayer: 0x14551ee40&; contentOffset: {0, 0}; contentSize: {0, 0}&
果然如此,UIScrollView在初始化的时候就已经把panGestureRecognizer.delegate设置成了自己。UIScrollView自带了两个手势,分别为:
UIPanGestureRecognizer
UIPinchGestureRecognizer
他们都是readonly的.
监听UIPanGestureRecognizer
手势是UIPanGestureRecognizer的属性,我们可以使用KVO来进行监听.
#import &RootViewController.h&
#define WIDTH
self.view.frame.size.width
#define HEIGHT
self.view.frame.size.height
@interface RootViewController ()&UIScrollViewDelegate&
@property (nonatomic, strong) UIScrollView
@implementation RootViewController
- (void)viewDidLoad
[super viewDidLoad];
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
_scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT * 3);
[self.view addSubview:_scrollView];
[_scrollView addObserver:self
forKeyPath:@&panGestureRecognizer.state&
options:NSKeyValueObservingOptionNew
context:nil];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
// 监听pan手势开始
if (_scrollView.panGestureRecognizer.state == UIGestureRecognizerStateBegan)
NSLog(@&UIGestureRecognizerStateBegan&);
// 监听pan手势值改变
if (_scrollView.panGestureRecognizer.state == UIGestureRecognizerStateChanged)
NSLog(@&UIGestureRecognizerStateChanged&);
// 监听pan手势结束
if (_scrollView.panGestureRecognizer.state == UIGestureRecognizerStateEnded)
NSLog(@&UIGestureRecognizerStateEnded&);
- (void)dealloc
[_scrollView removeObserver:self
forKeyPath:@&panGestureRecognizer.state&];
核心代码如下:
本人在测试的时候发现,一个完整的手势事件,一般情况下,手势值的改变是会执行多次的,而UIScrollview中的pan手势处理过后,手势开始,手势值改变以及手势结束,均只执行一回.
添加UISwipeGestureRecognizer手势
我们可以给UIScrollview添加UISwipeGestureRecognizer手势来判断是往哪个方向轻轻滑动了.
#import &RootViewController.h&
#import &GestureView.h&
@interface RootViewController ()&UIGestureRecognizerDelegate&
@implementation RootViewController
- (void)viewDidLoad
[super viewDidLoad];
#define WIDTH
self.view.frame.size.width
#define HEIGHT
self.view.frame.size.height
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.backgroundColor = [UIColor grayColor];
scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT*3);
[self.view addSubview:scrollView];
UISwipeGestureRecognizer *up = \
[[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(swipeEvent:)];
up.direction = UISwipeGestureRecognizerDirectionUp;
up.delegate =
[scrollView addGestureRecognizer:up];
UISwipeGestureRecognizer *down = \
[[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(swipeEvent:)];
down.direction = UISwipeGestureRecognizerDirectionD
down.delegate =
[scrollView addGestureRecognizer:down];
- (void)swipeEvent:(UISwipeGestureRecognizer *)gesture
NSLog(@&%@&, gesture);
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
return YES;
我直接将手势对象添加进category中,运行时关联上手势对象:
UIScrollView+Swipe.h + UIScrollView+Swipe.m
#import &UIKit/UIKit.h&
typedef enum {
@protocol UIScrollViewSwipeProtocol &NSObject&
- (void)swipeDirection:(EDirection)
@interface UIScrollView (Swipe)
@property (nonatomic, assign)
id&UIScrollViewSwipeProtocol&
@property (nonatomic, strong, readonly) UISwipeGestureRecognizer *upG
@property (nonatomic, strong, readonly) UISwipeGestureRecognizer *downG
- (void)activateSwipeG
- (void)cancelSwipeG
#import &UIScrollView+Swipe.h&
#import &objc/runtime.h&
@interface UIScrollView ()&UIGestureRecognizerDelegate&
@property (nonatomic, strong, readwrite) UISwipeGestureRecognizer *upG
@property (nonatomic, strong, readwrite) UISwipeGestureRecognizer *downG
static char swipeProtocolF
static char upGestureF
static char downGestureF
@implementation UIScrollView (Swipe)
- (id&UIScrollViewSwipeProtocol&)swipeProtocol
return objc_getAssociatedObject(self, &swipeProtocolFlag);
- (void)setSwipeProtocol:(id&UIScrollViewSwipeProtocol&)swipeProtocol
objc_setAssociatedObject(self, &swipeProtocolFlag,
swipeProtocol, OBJC_ASSOCIATION_ASSIGN);
- (void)setUpGesture:(UISwipeGestureRecognizer *)upGesture
objc_setAssociatedObject(self, &upGestureFlag,
upGesture, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- (UISwipeGestureRecognizer *)upGesture
return objc_getAssociatedObject(self, &upGestureFlag);
- (void)setDownGesture:(UISwipeGestureRecognizer *)downGesture
objc_setAssociatedObject(self, &downGestureFlag,
downGesture, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- (UISwipeGestureRecognizer *)downGesture
return objc_getAssociatedObject(self, &downGestureFlag);
- (void)activateSwipeGesture
if (self.upGesture == nil)
self.upGesture = \
[[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(swipeGestureEvents:)];
self.upGesture.direction = UISwipeGestureRecognizerDirectionUp;
self.upGesture.delegate =
[self addGestureRecognizer:self.upGesture];
if (self.downGesture == nil)
self.downGesture = \
[[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(swipeGestureEvents:)];
self.downGesture.direction = UISwipeGestureRecognizerDirectionD
self.downGesture.delegate =
[self addGestureRecognizer:self.downGesture];
- (void)swipeGestureEvents:(UISwipeGestureRecognizer *)gesture
if (self.swipeProtocol)
if (self.downGesture == gesture)
[self.swipeProtocol swipeDirection:E_DOWN];
if (self.upGesture == gesture)
[self.swipeProtocol swipeDirection:E_UP];
- (void)cancelSwipeGesture
[self removeGestureRecognizer:self.upGesture];
[self removeGestureRecognizer:self.downGesture];
self.upGesture
self.downGesture =
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
return YES;
然后这么使用即可:),so easy!
本文已收录于以下专栏:
相关文章推荐
//scrollView滚动时,就调用该方法。任何offset值改变都调用该方法。即滚动过程中,调用多次
- (void)scrollViewDidScroll:(UIScrollView *)s...
一、UIScrollView滚动视图
#import &MainViewController.h&
@interface
MainViewController ...
利用UIScrollView的滚动效果来实现,先上图:
实现过程是:在viewController里先加入UIScrollView和UIPageControl:
使用UIScrollView实现键盘自适应
UIScrollView学习
他的最新文章
讲师:王哲涵
讲师:韦玮
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)

我要回帖

更多关于 笔记本如何禁掉键盘 的文章

 

随机推荐