1

我正在尝试在两个方向上滑动视图,并且以下代码对于整个视图都可以正常工作,但是我需要此平移手势仅适用于左右角,每边仅说 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;
}
}
4

2 回答 2

0

UIView拿到using的四个角yourView.frame 然后,拿到里面的触摸点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));
}

希望这可以帮助 (-:

于 2013-07-02T09:47:48.040 回答
0

重写中的方法- (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;
}

希望能帮助到你。

于 2013-07-02T10:20:29.853 回答