简体   繁体   English

如何限制UIPanGestureRecognizer仅在角落工作

[英]How to limit the UIPanGestureRecognizer to work on corners only

I am trying to slide the view in both directions,and following code is working fine for entire view, but i need this pan gesture works only for left and right corners only say 50 pixels on each side, and neglect the gesture if its not on the corners. 我正在尝试在两个方向上滑动视图,并且以下代码对于整个视图都可以正常工作,但是我需要此平移手势仅适用于左右角,每侧只能说50像素,并且如果手势不在该位置,则忽略该手势角落。

please help 请帮忙

-(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;
}
}

Get the four corners of the UIView using yourView.frame Then, get the point of touch inside the UIView . 使用yourView.frame获取UIView四个角,然后获取UIView内部的接触点。 See the difference between the point of touch and each of the four corners. 查看触摸点与四个角中每个角之间的区别。 If any one of the four values is < 50 (say), do what you want in the method which is called through the UIPanGestureRecognizer . 如果四个值中的任何一个<50(例如),请执行通过UIPanGestureRecognizer调用的方法中想要的UIPanGestureRecognizer

You can the point of touch through 您可以通过触摸点

CGPoint currentlocation = [recognizer locationInView:self.view];

You can get the four corners through, 您可以通过四个角落,

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.

You can find the distance between two points through this method, 您可以通过这种方法找到两点之间的距离,

- (double) getDistance:(CGPoint)one :(CGPoint)two
{
    return sqrt((two.x - one.x)*(two.x - one.x) + (two.y - one.y)*(two.y - one.y));
}

Hope this helps (-: 希望这可以帮助 (-:

Override the method - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event in the UIView to look like the following 覆盖的方法- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)eventUIView看起来像以下

- (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;
}

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM