悬浮球

需求:
(1)可移动
(2)可点击

import UIKit

class DynamicDetailsPageSuspensionBall: UIButton {

    // MARK: - 生命周期
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.red
        self.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
        let panGesture = UIPanGestureRecognizer.init(target: self, action: #selector(self.panGesture(recognizer:)))
        self.addGestureRecognizer(panGesture)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 事件
    //移动手势
    @objc func panGesture(recognizer:UIPanGestureRecognizer) {
        let translationPoint = recognizer.translation(in: self.superview)
        let center = recognizer.view?.center
        recognizer.view?.center = CGPoint.init(x: (center?.x)! + translationPoint.x, y: (center?.y)! + translationPoint.y)
        recognizer.setTranslation(CGPoint.zero, in: self.superview)
    }
    //点击事件
    @objc func btnAction() {
        
    }

}

你可能感兴趣的:(悬浮球)