圆形视图上的 UIGestureRecognizer
UIGestureRecognizer on a rounded view
我有一个 UIView
,我使用下面的代码使它变圆。
[myView setBackgroundColor:[UIColor redColor]];
[[myView layer] setCornerRadius:[myView bounds].size.height / 2.0f];
[[myView layer] setMasksToBounds:YES];
[myView setClipsToBounds:YES];
然后添加UIPanGestureRecognizer
移动方块
UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(boxIsMoving:)];
[myView addGestureRecognizer:panGesture];
但问题是,当用户点击圆外但在实际框架中并开始拖动时,我的视图也开始移动。谁能建议我如何忽略回合外的触球。
您可以使用此委托方法。如果触摸在拐角半径之外,它将 return 否。
-(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;
}
正如 Erik Dolor 所建议的那样,您可以只计算手势与视图中心点之间的距离。使用这样的东西;
- (IBAction)boxIsMoving:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint viewCentre = CGPointMake(myView.bounds.size.width / 2, myView.bounds.size.height / 2);
CGPoint gesturePosition = [gestureRecognizer locationInView:self.view];
float distanceGestureToViewCentre = abs((sqrt((viewCentre.x - gesturePosition.x)
* (viewCentre.x - gesturePosition.x)
+ (viewCentre.y - gesturePosition.y)
* (viewCentre.y - gesturePosition.y))));
if(distanceTapToViewCentre < radiusOfView)
{
// Handle pan
}
else
{
// Do nothing
}
}
我有一个 UIView
,我使用下面的代码使它变圆。
[myView setBackgroundColor:[UIColor redColor]];
[[myView layer] setCornerRadius:[myView bounds].size.height / 2.0f];
[[myView layer] setMasksToBounds:YES];
[myView setClipsToBounds:YES];
然后添加UIPanGestureRecognizer
移动方块
UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(boxIsMoving:)];
[myView addGestureRecognizer:panGesture];
但问题是,当用户点击圆外但在实际框架中并开始拖动时,我的视图也开始移动。谁能建议我如何忽略回合外的触球。
您可以使用此委托方法。如果触摸在拐角半径之外,它将 return 否。
-(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;
}
正如 Erik Dolor 所建议的那样,您可以只计算手势与视图中心点之间的距离。使用这样的东西;
- (IBAction)boxIsMoving:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint viewCentre = CGPointMake(myView.bounds.size.width / 2, myView.bounds.size.height / 2);
CGPoint gesturePosition = [gestureRecognizer locationInView:self.view];
float distanceGestureToViewCentre = abs((sqrt((viewCentre.x - gesturePosition.x)
* (viewCentre.x - gesturePosition.x)
+ (viewCentre.y - gesturePosition.y)
* (viewCentre.y - gesturePosition.y))));
if(distanceTapToViewCentre < radiusOfView)
{
// Handle pan
}
else
{
// Do nothing
}
}