iOS判断一个点是否在一个区域内


有时候在做效果的时候,今天我在做点击出现冰裂开效果的时候,需要知道点击的位置,开始网上看了一下发现有一种方法判断

- (void)viewDidLoad
{
    [super viewDidLoad];
     
    CGMutablePathRef pathRef=CGPathCreateMutable();
    CGPathMoveToPoint(pathRef, NULL, 4, 4);
    CGPathAddLineToPoint(pathRef, NULL, 4, 14);
    CGPathAddLineToPoint(pathRef, NULL, 14, 14);
    CGPathAddLineToPoint(pathRef, NULL, 14, 4);
    CGPathAddLineToPoint(pathRef, NULL, 4, 4);
    CGPathCloseSubpath(pathRef);
     
    CGPoint point=CGPointMake(5, 5);
    CGPoint outPoint=CGPointMake(1, 1);
     
    if (CGPathContainsPoint(pathRef, NULL, point, NO))
    {
        NSLog(@"point in path!");
    }
     
    if (!CGPathContainsPoint(pathRef, NULL, outPoint, NO))
    {
        NSLog(@"outPoint out path!");
    }
}

这个其实也是可以的,但是有一个问题 如果好几个区域,pathref如何存在数组中很麻烦 这个可以看看

http://www.dapps.net/dev/iphone/how-to-create-a-simple-magazine-app-with-core-text.html

 于是我换一种方式 还是九宫格,我全部是UIVIew,这样的话我可以做的就是判断了。

判断的关键代码

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.superview];
    if (!_islock) {
        self.point = touchPoint;
        self.num = 1;
        _islock = YES;
    }
    if (_pointTap) {
        _pointTap ();
    }
}

这句话就是出现的是在父视图中的坐标。

    CGPoint touchPoint = [touch locationInView:self.superview];
然后就可以进行判断和加载了。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值