设置响应链

开发过程中会要求设置一些超出父视图的view 正常情况下子视图的超出部分是不举报交互能力的,此时想要超出部分具有交互能力,就应该设置响应链将交互传递给子视图



重写hitTest方法

    //控制响应链

  override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {       

if (!self.isUserInteractionEnabled || self.isHidden || self.alpha <= 0.01 ){         

  return nil       

}     

  let resultView  = super.hitTest(point, with: event)       

if resultView != nil {         

  return resultView   

    }else{         

  for subView in self.subviews.reversed() {             

  let convertPoint : CGPoint = subView.convert(point, from: self)             

  let hitView = subView.hitTest(convertPoint, with: event)           

    if (hitView != nil) {                   

return hitView               

}         

  }       

}     

  return nil   

}


    open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {         var bounds = self.bounds        

let widthDelta = max(17 - bounds.width, 0)      

  let heightDelta = max(17 - bounds.height, 0)      

  bounds = bounds.insetBy(dx: -widthDelta, dy: -heightDelta)      

  return bounds.contains(point)    

}

你可能感兴趣的:(设置响应链)