圆角视图上的UIGestureRecognizer

Tap*_*Pal 1 objective-c rounded-corners uigesturerecognizer ios uipangesturerecognizer

我有一个UIView和我使用下面的代码来实现它.

[myView setBackgroundColor:[UIColor redColor]];
[[myView layer] setCornerRadius:[myView bounds].size.height / 2.0f];
[[myView layer] setMasksToBounds:YES];
[myView setClipsToBounds:YES];
Run Code Online (Sandbox Code Playgroud)

然后添加 UIPanGestureRecognizer移动框

UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(boxIsMoving:)];
        [myView addGestureRecognizer:panGesture];
Run Code Online (Sandbox Code Playgroud)

但问题是当用户在该轮之外点击但在实际框架中并开始拖动我的视图也开始移动.任何人都可以建议我怎么能忽略这一轮之外的接触.

Uts*_*ikh 5

您可以使用此委托方法.如果触摸位于拐角半径之外,它将返回NO.

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint  touchPoint = [touch locationInView:myView];
    if (CGRectContainsPoint(myview.bounds, touchPoint))
    {
        CGFloat centerX = CGRectGetMidX(myView.bounds);
        CGFloat centerY = CGRectGetMidY(myView.bounds);
        CGFloat radius2 = pow((touchPoint.x -centerX),2)+ pow((touchPoint.y - centerY), 2);
        if (radius2 < pow(CGRectGetWidth(myView.frame)/2, 2))
        {
            return YES;
        }
    }
    return NO;
}
Run Code Online (Sandbox Code Playgroud)