带有圆角 UIKit 的 UIImageView 的可点击区域

问题描述

我遇到了一个有趣的问题 - 有一个带圆角的方形 UIImageView(使它成为一个圆形),我做了它,所以每当你触摸视图时就会发生一些事情,但问题是圆外和内的区域视图的框架仍然可以点击,我不知道如何修复它。

明确地说,我希望我的视图只能在圆圈内点击。有办法吗?

这是我的代码片段:

src/
  components/
    ParentComp/
      ParentComp.js
      components/
        ScopedComp.js

解决方法

将视图控制器指定为识别器的代理,并过滤识别器接收到的触摸:

func viewDidLoad() {
    ...
    tap.delegate = self
}

func gestureRecognizer(_ gr: UIGestureRecognizer,shouldReceive touch: UITouch) -> Bool {
    let bezierPath = UIBezierPath(roundedRect: profilePictureView.bounds,cornerRadius: profilePictureView.layer.cornerRadius)
    let point = touch.location(in: profilePictureView)
    return bezierPath.contains(point)
}