[英]How to limit the UIPanGestureRecognizer to work on corners only
我正在尝试在两个方向上滑动视图,并且以下代码对于整个视图都可以正常工作,但是我需要此平移手势仅适用于左右角,每侧只能说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;
}
}
使用yourView.frame
获取UIView
四个角,然后获取UIView
内部的接触点。 查看触摸点与四个角中每个角之间的区别。 如果四个值中的任何一个<50(例如),请执行通过UIPanGestureRecognizer
调用的方法中想要的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));
}
希望这可以帮助 (-:
覆盖的方法- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
中UIView
看起来像以下
- (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;
}
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.