美文网首页iOS 进阶
iOS多Window手势处理(Window手势传递)

iOS多Window手势处理(Window手势传递)

作者: 吃货_X | 来源:发表于2020-08-27 10:36 被阅读0次

    先上代码,这也是我的处理方法(有不对地方请指教)

    //CallWindow当前悬浮窗window,此window是全屏的,只有上面的view才是缩小(悬浮)的。
    override public func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
      //判断手势是否点悬浮view上(我的是电话view)
      if isTouchCallView(point: point) {
            //当前window响应
            return super.hitTest(point, with: event)
      }
      for keyWindow in UIApplication.shared.windows.reversed() {
            if (keyWindow is CallWindow) == false {
                 //倒序遍历取到上一个window响应
                 return keyWindow.hitTest(point, with: event)
            }
      }
      return super.hitTest(point, with: event)
    }
    
    当然其他的配套需要准备齐全:这些都是依葫芦画瓢
    public override func convert(_ point: CGPoint, to window: UIWindow?) -> CGPoint
    public override func convert(_ point: CGPoint, from window: UIWindow?) -> CGPoint
    public override func convert(_ rect: CGRect, to window: UIWindow?) -> CGRect
    public override func convert(_ rect: CGRect, from window: UIWindow?) -> CGRect
    
    

    判断点击:

     func isTouchCallView(point:CGPoint? = nil,rect:CGRect? = nil) ->(Bool) {
            
            //这儿写得就很low了,直接把悬浮view的frame存起来
            if let point = point,let array:[CGFloat] = (UserDefaults.standard.object(forKey: "CacheWithSuspensionViewFrame") as? [CGFloat]),array.count > 0 {
                let callRect = CGRect.init(x: array[0], y: array[1], width: array[2], height: array[3])
                if callRect.size.width == UIScreen.main.bounds.size.width {
                    return true
                }
                return callRect.contains(point)
            }
            
            if let rect = rect,let array:[CGFloat] = (UserDefaults.standard.object(forKey: "CacheWithSuspensionViewFrame") as? [CGFloat]),array.count > 0 {
                let callRect = CGRect.init(x: array[0], y: array[1], width: array[2], height: array[3])
                if callRect.size.width == UIScreen.main.bounds.size.width {
                    return true
                }
                return callRect.contains(rect)
            }
            return true
        }
    

    添加悬浮window:

    let window:CallWindow = CallWindow.init(frame: MainFrame)
    window.windowLevel = .alert + 1
    let controller = CallViewController.init()
    window.rootViewController = controller
    //window需要被持有,否则只会闪显(显示1秒后就消失),我这儿是被单例持有。
    self.callWindow = window
    self.callWindow?.makeKeyAndVisible()
    
    

    删除悬浮window:

    for window in UIApplication.shared.windows.reversed() {
        if (window is CallWindow) == false {
            window.makeKeyAndVisible()
             break
        }
    }
    self.callWindow?.isHidden = true
    self.callWindow = nil
    
    

    为何今天想写着这个,也是有感而发!
    最近项目上需要做语音视频悬浮框,有两种实现方法:
    1、view加到kyewindow上
    2、另外添加一个window,设置makeKeyAndVisible()
    哎,说多了!一开始我是选择第一种,后面因为与其他功能冲突改为第二种。

    相关文章

      网友评论

        本文标题:iOS多Window手势处理(Window手势传递)

        本文链接:https://www.haomeiwen.com/subject/saxcsktx.html