ios 悬浮小球移动 (swift)

2019-04-26  本文已影响0人  Enter_Y

extension ELAudioFloatPlayerView {

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
        let offsetY = currentPosition.y - touchP.y
        
      //移动后的按钮中心坐标
        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

        //x轴左右极限坐标
        if btnX > superViewWidth {
            //按钮右侧越界
            let centerX = superViewWidth - btnW/2
            self.center = CGPoint(x: centerX, y: centerY)
        }else if (btnX < 0){
            //按钮左侧越界
            let centerX = btnW * 0.5
            self.center = CGPoint(x: centerX, y: centerY)
        }

        //默认都是有导航条的,有导航条的,父试图高度就要被导航条占据,固高度不够
        let defaultNaviHeight:CGFloat = 64
        let judgeSuperViewHeight = superViewHeight - defaultNaviHeight

        //y轴上下极限坐标
        if btnY <= 0 {
            //按钮顶部越界
            centerY = btnH * 0.7
            self.center = CGPoint(x: centerX, y: centerY)
        } else if btnY > judgeSuperViewHeight {
            //按钮底部越界
            let y = superViewHeight - btnH * 0.5
            self.center = CGPoint(x:btnX, 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)
    }

    //按钮靠近右侧
    switch (type) {
    case .none:
        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)
            }
        }
    case .left:
        UIView.animate(withDuration: 0.5) {
            let btnX:CGFloat = 0
            self.frame =  CGRect(x: btnX, y: btnY, width: btnWidth, height: btnHeight)
        }
    case .right:
        UIView.animate(withDuration: 0.5) {
            let btnX = self.superview!.frame.size.width - btnWidth
            self.frame =  CGRect(x: btnX, y: btnY, width: btnWidth, height: btnHeight)
        }
    }
}

}

上一篇下一篇

猜你喜欢

热点阅读