我有一个包含另一个UIImage的掩码的UIImage。唯一的问题是,当我点击面具外面的区域时,脚本仍然认为我在点击图像。

let tap = UITapGestureRecognizer()
override func viewDidLoad() {
    super.viewDidLoad()
tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(_:)))
tap.numberOfTapsRequired = 1
tap.numberOfTouchesRequired = 1

let imageMask = UIImageView()
imageMask.image = ...//Image to mask to
imageMask.frame = imageView.bounds

imageView = UIImageView(image: ...)
imageView.mask = imageMask

imageView.addGestureRecognizer(tap)
}

@objc func handleTap(_ sender: UITapGestureRecognizer) {
    print("hello")
    //prints "hello" when tapped Image outside of mask
}

如何将图像完全裁剪到遮罩上,使其不会在遮罩外注册抽头?

最佳答案

用这个代替两个图像视图

let overMask = UIView()
overMask.frame = imageView.bounds
overMask.isUserInteractionEnabled = true

imageView = UIImageView(image: ...)
imageView.mask = overMask

imageView.addGestureRecognizer(tap)

overMask.addSubview(imageView)

10-08 05:33
查看更多