如何限制 UIPanGestureRecognizer 仅在角落工作
Posted
技术标签:
【中文标题】如何限制 UIPanGestureRecognizer 仅在角落工作【英文标题】:How to limit the UIPanGestureRecognizer to work on corners only 【发布时间】:2013-07-02 09:41:00 【问题描述】:我正在尝试在两个方向上滑动视图,并且以下代码对于整个视图都可以正常工作,但是我需要此平移手势仅适用于左右角,每边仅说 50 像素,并且忽略手势如果它不在角落里。
请帮忙
-(void)setupGestures
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(movePanel:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[_centerViewController.view addGestureRecognizer:panRecognizer];
-(void)movePanel:(id)sender
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
CGPoint velocity = [(UIPanGestureRecognizer*)sender velocityInView:[sender view]];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
UIView *childView = nil;
if(velocity.x > 0)
if (!_showingRightPanel)
childView = [self getLeftView];
else
if (!_showingLeftPanel)
childView = [self getRightView];
// make sure the view we're working with is front and center
[self.view sendSubviewToBack:childView];
[[sender view] bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
if(velocity.x > 0)
// NSLog(@"gesture went right");
else
// NSLog(@"gesture went left");
if (!_showPanel)
[self movePanelToOriginalPosition];
else
if (_showingLeftPanel)
[self movePanelRight];
else if (_showingRightPanel)
[self movePanelLeft];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged)
if(velocity.x > 0)
// NSLog(@"gesture went right");
else
// NSLog(@"gesture went left");
// are we more than halfway, if so, show the panel when done dragging by setting this value to YES (1)
_showPanel = abs([sender view].center.x - _centerViewController.view.frame.size.width/2) > _centerViewController.view.frame.size.width/2;
// allow dragging only in x coordinates by only updating the x coordinate with translation position
[sender view].center = CGPointMake([sender view].center.x + translatedPoint.x, [sender view].center.y);
[(UIPanGestureRecognizer*)sender setTranslation:CGPointMake(0,0) inView:self.view];
// if you needed to check for a change in direction, you could use this code to do so
if(velocity.x*_preVelocity.x + velocity.y*_preVelocity.y > 0)
// NSLog(@"same direction");
else
// NSLog(@"opposite direction");
_preVelocity = velocity;
【问题讨论】:
为什么不创建自定义手势识别器来封装逻辑? 【参考方案1】:使用yourView.frame
获取UIView
的四个角
然后,获取UIView
内的接触点。
查看触摸点和四个角中的每一个之间的差异。
如果四个值中的任何一个小于 50(例如),请在通过 UIPanGestureRecognizer
调用的方法中执行您想要的操作。
你可以通过触摸点
CGPoint currentlocation = [recognizer locationInView:self.view];
你可以通过四个角,
CGPoint topLeft = view.bounds.origin;
topLeft = [[view superview] convertPoint:topLeft fromView:view];
CGPoint topRight = CGPointMake(view.bounds.origin.x + view.bounds.width, view.bounds.origin.y);
topRight = [[view superview] convertPoint:topRight fromView:view];
// and so on.
通过这个方法可以求出两点之间的距离,
- (double) getDistance:(CGPoint)one :(CGPoint)two
return sqrt((two.x - one.x)*(two.x - one.x) + (two.y - one.y)*(two.y - one.y));
希望这会有所帮助(-:
【讨论】:
【参考方案2】:覆盖UIView
中的- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
方法,如下所示
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
BOOL pointInside = NO;
if(CGRectContainsPoint(self.bounds, point) && ((point.x < 50) || (point.x > self.bounds.size.width-50)))
pointInside = YES;
return pointInside;
希望对你有帮助。
【讨论】:
以上是关于如何限制 UIPanGestureRecognizer 仅在角落工作的主要内容,如果未能解决你的问题,请参考以下文章