首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift -如何识别子视图的手势交互,哪个部分位于父视图的外部

Swift -如何识别子视图的手势交互,哪个部分位于父视图的外部
EN

Stack Overflow用户
提问于 2021-03-19 19:47:35
回答 1查看 37关注 0票数 0

我为不同的形状创建了锚点,这有助于调整形状的大小。这工作得很好,但问题出在锚点手势识别器区域(浅蓝色部分)。当锚点在形状内按下时,您可以移动锚点,但不能选择红色区域来移动锚点,因为它位于父视图之外。

我的问题是,有没有一种方法可以识别红色区域,因为它是形状的子区域,但红色区域位于父视图之外?

我想也许我可以扩展形状的帧大小,包括锚点,并重新计算形状区域,删除锚点的宽度,但不确定这是否是最好的方法。

下面是附加到形状视图的锚点视图

代码语言:javascript
复制
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)
  }
}
EN

回答 1

Stack Overflow用户

发布于 2021-03-19 19:59:36

代码语言:javascript
复制
  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
   }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66707589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档