如何解决Swift - 如何识别子视图的手势交互哪个部分位于父视图之外
我为不同的形状创建了锚点,这有助于调整形状的大小。这工作正常,但问题在于锚点手势识别器区域(浅蓝色部分)。您可以在形状内按下锚点时移动锚点,但无法选择红色区域来移动锚点,因为它位于父视图之外。
我的问题是,有没有办法识别红色区域,因为它是形状的子级,但红色区域位于父视图之外?
我想也许我会扩展形状的框架大小以包括锚点并重新计算形状区域,去除锚点的宽度,但不确定这是否是最好的方法。
下面是附加到形状视图的锚点视图
class AnchorPoint: UIView {
var panGestureStartLocation: CGPoint?
var dot = UIView()
var positionName: String = ""
func config(positionName: String,position: CGPoint){
self.positionName = positionName
self.frame = CGRect(x: 0,y: 0,width: 60,height: 60)
self.layer.cornerRadius = self.frame.height / 2
self.backgroundColor = UIColor.systemBlue.withAlphaComponent(0.5)
self.center = position
self.isUserInteractionEnabled = true
}
func addDot(){
dot = UIView()
dot.frame = CGRect(x: (self.bounds.size.width / 2) - 10,y: (self.bounds.size.height / 2) - 10,width: 20,height: 20)
dot.layer.cornerRadius = 20 / 2
dot.backgroundColor = UIColor.green.withAlphaComponent(0.6)
dot.layer.borderWidth = 2
dot.layer.borderColor = UIColor.blue.cgColor
dot.isUserInteractionEnabled = true
addSubview(dot)
}
}
解决方法
override func hitTest(_ point: CGPoint,with event: UIEvent?) -> UIView? {
var hitView = super.hitTest(point,with: event)
if hitView == nil {
let npoint = dot.convert(point,from: self)
hitView = dot.hitTest(npoint,with: event)
}
return nil
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。