iOS有时候需要判断是否touch到某个图的区域中。这里做了个小示例,通过CGPath创建一个区域,区域是由路径做两点间线段并闭合成的区域,比如这里创建了一个简单的矩形。然后就可以用CGPath相关函数判断点是否在区域里了。

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

相关文章:

  • 2021-08-23
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2021-07-02
猜你喜欢
  • 2022-12-23
  • 2021-11-07
  • 2021-06-03
  • 2022-12-23
  • 2022-12-23
  • 2021-04-21
  • 2021-12-18
相关资源
相似解决方案