直接上代碼
//
// 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")
}
}
/// 懸浮窗口功能實(shí)現(xiàn)
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
//移動(dòng)后的按鈕中心坐標(biāo)
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軸左右極限坐標(biāo)
if btnX > superViewWidth {
//按鈕右側(cè)越界
let centerX = superViewWidth - btnW
self.center = CGPoint(x: centerX, y: centerY )
}else if (btnX < 0){
//按鈕左側(cè)越界
let centerX = btnW * 0.5
self.center = CGPoint(x: centerX, y: centerY)
}
let judgeSuperViewHeight = superViewHeight - GlobalTabBarAndSafeAreaBottomHeight - btnH*0.5
//y軸上下極限坐標(biāo)
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
//結(jié)束move的時(shí)候陕见,計(jì)算移動(dòng)的距離是>最低要求甥温,如果沒(méi)有,就調(diào)用按鈕點(diǎn)擊事件
let isOverX = abs(btnX - touchX) > minDistance
let isOverY = abs(btnY - touchY) > minDistance
if isOverX || isOverY {
//超過(guò)移動(dòng)范圍就不響應(yīng)點(diǎn)擊 - 只做移動(dòng)操作
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)
}
}
}
}