我有一个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
}
我如何将图像完全裁剪到蒙版上,这样它就不会在蒙版外面记录水龙头了?
答案 0 :(得分:2)
使用此图像代替两个imageViews
let overMask = UIView()
overMask.frame = imageView.bounds
overMask.isUserInteractionEnabled = true
imageView = UIImageView(image: ...)
imageView.mask = overMask
imageView.addGestureRecognizer(tap)
overMask.addSubview(imageView)