UIPanGestureRecognizer 在视图中使用 UIScrollView

Posted

技术标签:

【中文标题】UIPanGestureRecognizer 在视图中使用 UIScrollView【英文标题】:UIPanGestureRecognizer at view with UIScrollView 【发布时间】:2015-06-26 09:34:43 【问题描述】:

我对@9​​87654322@ 有意见。

UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handlePan:)];
    [recognizer setMaximumNumberOfTouches:1];
    [recognizer setDelegate:self];

[self.view addGestureRecognizer: recognizer];

=======
-(void) handlePan:(UIPanGestureRecognizer*)gestureRecognizer

  move subview of self.view

子视图里面有一个滚动视图。当滚动视图处于水平滚动末尾时,如何使用事件 -handlePan: 捕获手势?

【问题讨论】:

设置 UIPanGestureRecognizer 为滚动视图,如果你想捕捉滚动视图点击手势 【参考方案1】:

您可以使用以下功能:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

【讨论】:

【参考方案2】:

我从上周开始使用 panGesture。在我看来,这里有两个问题;

1) 用户体验设计。 UIScrollView : UIView 是“触摸”可识别的 - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view;

UIPangestureRecognizer 还检测“触摸”。

- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view; // the location of a particular touch

从用户体验的角度来看,您如何指出 ONE UI 区域中的两种触摸之间的差异,纯粹是出于设计方面的考虑。

2) 代码。看起来你只是想平移一个视图(UIScrollView,或其他 UIView 的视图)

UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];

// no need to set the number of touches to 1. 1 is default number. 
yourView = [[UIImageView alloc]init]; // or initWithFrame
// add the recognizer to your view 
[youView addGestureRecognizer:recognizer];
//add this view to the mainView
[self addSubview: yourView];
// then process @selector(handlePan:)
-(void) handlePan:(UIPanGestureRecognizer*)gestureRecognizer
 
// this method is to deal with location of the panGesture itself, not other third views.
CGPoint touchLocation = [gestureRecognizer locationInView:self]; 
yourView.center = touchLocation;

【讨论】:

以上是关于UIPanGestureRecognizer 在视图中使用 UIScrollView的主要内容,如果未能解决你的问题,请参考以下文章

UIPanGestureRecognizer 碰撞

如何在父视图中限制 UIPanGestureRecognizer

UIPanGestureRecognizer 干扰滚动

未调用 iOS 13 UIPanGestureRecognizer 选择器

Swift:获取 UIPanGestureRecognizer 的目标

当对象移动到某个帧时如何停止 UIPanGestureRecognizer