美文网首页
Swift 虚拟摇杆

Swift 虚拟摇杆

作者: 林麟霖 | 来源:发表于2020-03-19 09:49 被阅读0次

Swift 虚拟摇杆
网上能找到的摇杆都是oc或者cocos2dx,在swift项目里用着很不方便.
自写了一个swift 极简版摇杆 swift4
直接上代码 RockerView.swift


import UIKit
protocol RockerDelegate:NSObjectProtocol{
    func onDrag(x:CGFloat , y:CGFloat)
}
class RockerView: UIView ,UIGestureRecognizerDelegate{
    
    var delegate:RockerDelegate?//拖拽接口

    private let ScreenHeight = UIScreen.main.bounds.size.height
    private let ScreenWidth = UIScreen.main.bounds.size.width
    
    
//    private let cornerRadio:CGFloat = 0//按钮圆角
    private let ballX:CGFloat = 40  //按钮x半径
    private let ballY:CGFloat = 40  //按钮y半径
    
    private var centerPoint:CGPoint = CGPoint(x: 180, y: 180)  //初始中心
    private var radius:CGFloat = 80  //拖拽范围半径

    private var ballBtn:UIButton?
    private var currentCenter:CGPoint?//拖拽时的中心
    private var panEndCenter:CGPoint = CGPoint(x: 0, y: 0)
//    private var mTimer:Timer?
    
//MARK:- init method
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: CGFloat(2 * ballX), height: CGFloat(2 * ballY)))
        self.addSubview(self.getBallBtn())
        self.backgroundColor = UIColor.clear
        self.currentCenter = centerPoint //初始位置
        self.calculateShowCenter(point: self.currentCenter!)
        //跟随手指拖动
        let moveGes:UIPanGestureRecognizer = UIPanGestureRecognizer.init(target: self, action: #selector(self.dragBallView))
        self.addGestureRecognizer(moveGes)
//        //添加到window上
//        self.ww_getKeyWindow().addSubview(self)
//        //显示在视图的最上层
//        self.ww_getKeyWindow().bringSubviewToFront(self)

    }
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }
    
    //设置位置 半径
    func setCenter(point:CGPoint , radius:CGFloat) {
        self.centerPoint = point
        self.center = point
        self.radius = radius
        
    }
    
   deinit{
        NSLog("IMOSuspendedBallView deinit")
    }
    
// MARK:- other method
    var pressPoint:CGPoint?
    //跟随手指拖动
    @objc func dragBallView(panGes:UIPanGestureRecognizer) {
        let translation:CGPoint = panGes.translation(in: self.ww_getKeyWindow())
        if panGes.state == .began {
            pressPoint = self.center
        }
        
        pressPoint = CGPoint(x: pressPoint!.x+translation.x, y: pressPoint!.y+translation.y)
        self.center = getTranslationPoint(panPoint: pressPoint!)
        
        panGes .setTranslation(CGPoint(x: 0, y: 0), in: self.ww_getKeyWindow())
        if panGes.state == UIGestureRecognizer.State.ended{
//            self.panEndCenter = self.center
            pressPoint = nil
            self.caculateBallCenter()
            self.delegate?.onDrag(x: 0, y: 0)
        }
    }
    
    //计算是否超过范围
    func getTranslationPoint(panPoint:CGPoint) -> CGPoint {
        let radius =  CGFloat(sqrt((panPoint.x - centerPoint.x)*(panPoint.x - centerPoint.x) + (panPoint.y - centerPoint.y)*(panPoint.y - centerPoint.y)))

        var resultPoint = panPoint
        var xResult = panPoint.x
        var yResult = panPoint.y
        if radius > self.radius {
            xResult = centerPoint.x + (panPoint.x - centerPoint.x) * self.radius / radius;
            yResult = centerPoint.y + (panPoint.y - centerPoint.y) * self.radius / radius;

            resultPoint.x = xResult
            resultPoint.y = yResult
        }
        self.delegate?.onDrag(x: (xResult - centerPoint.x) / self.radius, y: (yResult-centerPoint.y)/self.radius)
        return resultPoint
    }
    
    //按钮返回中心
    func caculateBallCenter() {
        self.currentCenter = centerPoint
        unowned let weakSelf = self
        UIView.animate(withDuration: 0.2) {
            weakSelf.center = self.centerPoint
        }
    }
    
    //按钮位置移动
    func calculateShowCenter(point:CGPoint) {
        unowned let weakSelf = self
        UIView.animate(withDuration: 0.2) {
            weakSelf.center = CGPoint(x: point.x, y: point.y)
        }
    }
    
    //单列获取悬浮起按钮
    func getBallBtn() -> UIButton {
        if ballBtn == nil {
            ballBtn = UIButton.init(type: .custom)
            ballBtn?.setBackgroundImage(UIImage.init(named: "yuan"), for: .normal)
            ballBtn?.frame = CGRect(x: 0, y: 0, width: CGFloat(2 * ballX), height: CGFloat(2 * ballY))
            ballBtn?.layer.masksToBounds = true
//            ballBtn?.layer.cornerRadius = cornerRadio//圆角 用图做按钮的话可能不需要
            ballBtn?.center = self.center
            ballBtn?.addTarget(self, action: #selector(clickBallViewAction), for: .touchUpInside)
        }
        return ballBtn!
    }

    
//MARK:- action
    @objc func clickBallViewAction() {
        //摇杆点击事件
        print("点击摇杆")
    }
    
//MARK:- private utility
    func ww_getKeyWindow() -> UIWindow {
        if UIApplication.shared.keyWindow == nil {
            return ((UIApplication.shared.delegate?.window)!)!
        }else{
            return UIApplication.shared.keyWindow!
        }
    }
    
}


使用时

 设置好 RockerDelegate

        let ctrlBtn = RockerView()

        ctrlBtn.setCenter(point:controlView.center, radius: 40) //设置中心位置 和半径

        ctrlBtn.delegate = self 

        self.view.addSubview(ctrlBtn)

功能简单,有需要可以自行添加按下事件,点击事件等.

稍后更新截图,话说怎么截动态图..

转载注明原文链接

相关文章

网友评论

      本文标题:Swift 虚拟摇杆

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