美文网首页
swift 实现一个滑动悬浮窗

swift 实现一个滑动悬浮窗

作者: 鹏飞说 | 来源:发表于2023-08-08 10:34 被阅读0次

直接上代码


//
//  ZYFloatPlayerView.swift
//  SoChat
//
//  Created by APPLE on 2023/8/9.
//

import UIKit

class ZYFloatPlayerView: UIView {
    
    private var touchX : CGFloat = 0
    private var touchP : CGPoint = CGPoint(x: 0, y: 0)
    private var touchY : CGFloat = 0

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
/// 悬浮窗口功能实现
extension ZYFloatPlayerView {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        if let touch = touches.first {
            let touchPoint =  touch.location(in: self)
            touchX = touchPoint.x
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)
        if let touch = touches.first {
            let currentPosition =  touch.location(in: self)
            let offsetX = currentPosition.x - touchP.x - self.frame.size.width/2
            let offsetY = currentPosition.y - touchP.y - self.frame.size.height/2
            
          //移动后的按钮中心坐标
            let centerX = self.center.x + offsetX
            var centerY = self.center.y + offsetY
            self.center = CGPoint(x: centerX, y: centerY)
            //父试图的宽高
            let superViewWidth = self.superview!.frame.width
            let superViewHeight = self.superview!.frame.height
            ///
            
            let btnX = self.frame.origin.x
            let btnY = self.frame.origin.y
            let btnW = self.frame.size.width
            let btnH = self.frame.size.height
            
//            if centerY < GlobalNavAddStatusHeight + btnH/2 {
//                centerY = GlobalNavAddStatusHeight + btnH/2
//            }
//
//            if centerY > superViewHeight - GlobalTabBarAndSafeAreaBottomHeight - btnH/2 {
//                centerY = superViewHeight -  GlobalNavAddStatusHeight + btnH/2
//            }
            
            //x轴左右极限坐标
            if btnX > superViewWidth {
                //按钮右侧越界
                let centerX = superViewWidth - btnW
                self.center = CGPoint(x: centerX, y: centerY )
            }else if (btnX < 0){
                //按钮左侧越界
                let centerX = btnW * 0.5
                self.center = CGPoint(x: centerX, y: centerY)
            }
            
            let judgeSuperViewHeight = superViewHeight - GlobalTabBarAndSafeAreaBottomHeight - btnH*0.5

            //y轴上下极限坐标
            if btnY <= GlobalNavAddStatusHeight + btnH*0.5 {
                //按钮顶部越界
//                centerY = btnH * 0.5
                let x = centerX
                let y = btnH * 0.5 + GlobalNavAddStatusHeight // 底部距离 tabbar + 安全距离
                self.center = CGPoint(x: x, y: y)
            } else if btnY > judgeSuperViewHeight {
                //按钮底部越界
                let y = superViewHeight - btnH * 0.5
                let x = btnX + self.frame.size.height/2
                self.center = CGPoint(x:x, y:y)
            }
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let btnWidth = self.frame.size.width
        let btnHeight = self.frame.size.height
        let btnY = self.frame.origin.y
        let btnX = self.frame.origin.x
        
        let minDistance:CGFloat = 2

        //结束move的时候,计算移动的距离是>最低要求,如果没有,就调用按钮点击事件
        let isOverX = abs(btnX - touchX) > minDistance
        let isOverY = abs(btnY - touchY) > minDistance

        if isOverX || isOverY {
            //超过移动范围就不响应点击 - 只做移动操作
            super.touchesCancelled(touches, with: event)
        }else{
            super.touchesEnded(touches, with: event)
        }

        if self.center.x >= self.superview!.frame.size.width/2 {
            UIView.animate(withDuration: 0.5) {
                let btnX = self.superview!.frame.size.width - btnWidth
                self.frame =  CGRect(x: btnX, y: btnY, width: btnWidth, height: btnHeight)
            }
        } else {
            UIView.animate(withDuration: 0.5) {
                let btnX:CGFloat = 0
                self.frame =  CGRect(x: btnX, y: btnY, width: btnWidth, height: btnHeight)
            }
        }
    }
}

相关文章

网友评论

      本文标题:swift 实现一个滑动悬浮窗

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