悬浮球

作者: 古月思吉 | 来源:发表于2018-12-23 13:05 被阅读0次

    需求:
    (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() {
            
        }
    
    }
    

    相关文章

      网友评论

          本文标题:悬浮球

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