Determine if UIView is visible to the user?

前端 未结 12 1788
不知归路
不知归路 2020-12-07 15:21

is it possible to determine whether my UIView is visible to the user or not?

My View is added as subview several times into a Tab Bar

12条回答
  •  無奈伤痛
    2020-12-07 15:53

    try this:

    func isDisplayedInScreen() -> Bool
    {
     if (self == nil) {
         return false
      }
        let screenRect = UIScreen.main.bounds 
        // 
        let rect = self.convert(self.frame, from: nil)
        if (rect.isEmpty || rect.isNull) {
            return false
        }
        // 若view 隐藏
        if (self.isHidden) {
            return false
        }
    
        // 
        if (self.superview == nil) {
            return false
        }
        // 
        if (rect.size.equalTo(CGSize.zero)) {
            return  false
        }
        //
        let intersectionRect = rect.intersection(screenRect)
        if (intersectionRect.isEmpty || intersectionRect.isNull) {
            return false
        }
        return true
    }
    

提交回复
热议问题