Swift 给View添加手势封装

每次给View添加手势 都需要写很多代码,最常用的就是点击手势,虽然有按钮支持,但是对于自定义的控件添加手势就需要单独写一些烦躁的简单的代码,例如:

let imgView = UIImageView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
self.viewf.addSubView(imgView)

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(viewTapAction(gesture:)))
tapGesture.numberOfTouchesRequired = 1
tapGesture.numberOfTapsRequired = 1
self.addGestureRecognizer(tapGesture)

imgView.addGestureRecognizer(tapGesture)

下面就是我的一些对手势的简单的封装,基本满足日常所需

基本流程就是

  • 创建View的扩展类
  • 添加绑定
  • 添加回调
  • 完成

直接上代码

import Foundation
import UIKit

typealias viewAction = (UIGestureRecognizer)->()

//添加手势枚举
extension UIView {
    enum GestureENUM {
        case tap //点击
        case long //长按
        case pan //拖拽
        case roation //旋转
        case swipe //轻扫
        case pinch //捏合
    }
    
    private struct AssociatedKeys {
        static var actionKey = "gestureKey"
    }
    
    @objc dynamic var action:viewAction? {
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.actionKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
        }
        get {
            if let action = objc_getAssociatedObject(self, &AssociatedKeys.actionKey) as? viewAction {
                return action
            }
            return nil
        }
    }
    
    @objc func viewTapAction(gesture: UIGestureRecognizer) {
        if action != nil {
            action!(gesture)
        }
    }
    /// 添加手势 
    func addGesture( _ gesture : GestureENUM , response:@escaping viewAction) {
        
        self.isUserInteractionEnabled = true
        switch gesture {
        case .tap: //点击
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(viewTapAction(gesture:)))
            tapGesture.numberOfTouchesRequired = 1
            tapGesture.numberOfTapsRequired = 1
            self.addGestureRecognizer(tapGesture)
            self.action = response
        case .long: //长按
            let longPress = UILongPressGestureRecognizer(target: self, action: #selector(viewTapAction(gesture:)))
            self.addGestureRecognizer(longPress)
            self.action = response
        case .pan: //拖拽
            let panGesture = UIPanGestureRecognizer(target: self, action: #selector(viewTapAction(gesture:)))
            self.addGestureRecognizer(panGesture)
            self.action = response
        case .roation: // 旋转 
            let roation = UIRotationGestureRecognizer(target: self, action: #selector(viewTapAction(gesture:)))
            self.addGestureRecognizer(roation)
            self.action = response
        case .swipe: //轻扫
            let swipe = UISwipeGestureRecognizer(target: self, action: #selector(viewTapAction(gesture:)))
            self.addGestureRecognizer(swipe)
            self.action = response
        case .pinch: //捏合
            let pinch = UIPinchGestureRecognizer(target: self, action: #selector(viewTapAction(gesture:)))
            self.addGestureRecognizer(pinch)
            self.action = response
        }
    

使用方法

let view = UIView()
view.addGesture(.pan) { (recognizer) in
    // 实现方法
    <#code#>
}

你可能感兴趣的:(Swift 给View添加手势封装)