简体   繁体   English

如何检测圆形视图内的触摸

[英]how to detect touches inside a circular view

在此输入图像描述

I have a rounded UIView. 我有一个圆形的UIView。 I must detect the touches only inside the purple circle. 我必须只检测紫色圆圈内的触摸。 All the touches outside the circle,eg black square and white background must be neglected. 必须忽略圆圈外的所有触摸,例如黑色方形和白色背景。

Setting the radius and detecting the touches will not be any use, since when multiple views are on top of each other with different controllers, it will be difficult to manage. 设置半径和检测触摸将没有任何用处,因为当多个视图在彼此之上具有不同的控制器时,将难以管理。

Is there any way, I can do this. 有什么办法,我可以这样做。 Please could you give me some suggestions do this. 请你能给我一些建议。

Create a custom subclass of UIView , say CircularView and override the pointInside:withEvent: method to ignore points that lie outside the circle. 创建一个UIView的自定义子类,比如说CircularView并覆盖pointInside:withEvent:方法来忽略位于圆外的点。 An object of this subclass will be self contained and you can arrange it in whatever manner you desire. 该子类的一个对象将是自包含的,您可以按照您想要的任何方式进行排列。

To find out if a circular region contains a point or not, you can make use of the Core Graphics function CGPathContainsPoint or the containsPoint: method in UIBezierPath . 要找出一个圆形区域包含点或没有,你可以使用核芯显卡功能的CGPathContainsPointcontainsPoint:方法UIBezierPath That will require you to remember the CGPathRef or the UIBezierPath object that represents the circle. 这将要求您记住代表圆的CGPathRefUIBezierPath对象。 In this example, I am assuming that you've created a circular path using UIBezierPath and it is stored as a property on the CircularView class. 在此示例中,我假设您已使用UIBezierPath创建了一个循环路径,并将其存储为CircularView类的属性。

@interface CircularView : UIView

// initialize this when appropriate
@propery (nonatomic, strong) UIBezierPath *circularPath;

@end

@implementation CircularView

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    return [circularPath containsPoint:point];
}

@end

And that's it. 就是这样。

You can easily apply condition to touch If you have radius of the circle. 您可以轻松应用条件触摸如果您有圆的半径。 Check distance between touch point and center of the circle and check if distance is less than radius of circle then work on touch otherwise ignore it. 检查触摸点圆心之间的距离,检查距离是否小于圆的半径,然后触摸,否则忽略它。

You can calculate distance by using following method: 您可以使用以下方法计算距离:

-(float)distanceWithCenter:(CGPoint)current with:(CGPoint)SCCenter
{
    CGFloat dx=current.x-SCCenter.x;
    CGFloat dy=current.y-SCCenter.y;

    return sqrt(dx*dx + dy*dy);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 CGFloat radius=5;
 CGPoint centerOfCircle=CGPointMake(140,200);
 UITouch *touch=[touches anyObject];
 CGPoint touchPoint=[touch locationInView:self.view];

 CGFloat distance=[self distanceWithCenter:centerOfCircle with:touchPoint];

 if (distance<=radius) {
  //perform your tast.
 }
}

Create a subclass of UIView for your circle, then override PointInside like this: 为你的圈创建一个UIView的子类,然后像这样重写PointInside:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if (![super pointInside:point withEvent:event])
    {
        return NO;
    }
    BOOL isInside = (pow((point.x-self.frame.size.width/2), 2) + pow((point.y - self.frame.size.height/2), 2) < pow((self.frame.size.width/2), 2)) ? YES:NO;
    return isInside;
}

You can ditch the 'isInside' variabe, but this way is easier to test. 您可以放弃'isInside'变量,但这种方式更容易测试。

You could simply make a button and set it a custom size and make it circular like that and any time a user touches it, it can add 1 to an integer or float of the number of touches. 您可以简单地创建一个按钮并将其设置为自定义大小并使其像这样循环,并且每当用户触摸它时,它可以将1添加到触摸次数的整数或浮点数。 Simple as that. 就那么简单。

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

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