iOS点击图片不同的区域添加相应的响应事件

首先了解一下官方的API说明:

  1. /* Return true if point' is contained inrect', false otherwise. */
    CG_EXTERN bool CGRectContainsPoint(CGRect rect, CGPoint point)
  1. // individual UIGestureRecognizer subclasses may provide subclass-specific location information. see individual subclasses for details
  • (CGPoint)locationInView:(nullable UIView*)view; // a generic single-point location for the gesture. usually the centroid of the touches involved

方案:通过给图片添加点击手势,区分有效范围,实现不同位置的点击响应
例子:

- (void)tap:(UIGestureRecognizer *)ges {
    CGPoint location = [ges locationInView:self.view];
    NSLog(@"---x:%f   y:%f",location.x,location.y);
    
    for (AreaModel *areaModel in AreaList) {
        CGFloat originX = floor(self.view.width * areaModel.hotAreaX.floatValue);
        CGFloat originY = floor(self.view.height * areaModel.hotAreaY.floatValue);
        CGFloat width = floor(self.view.width * areaModel.hotAreaWidth.floatValue);
        CGFloat height = floor(self.view.height * areaModel.hotAreaHeight.floatValue);
        CGRect frame = CGRectMake(originX, originY, width, height);
        if(CGRectContainsPoint(frame, location)){
            NSLog(@"点中了-------------");
         
            return;
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容