MG--Swift3.0拖拽手势代替轻扫

  • 拖拽(UIPanGestureRecognizer)手势代替轻扫(UISwipeGestureRecognizer)手势的简单实现


class AESViewController: UIViewController {
    // 自定义属性
    var direction: UIPanGestureRecognizerDirection = .undefined
    fileprivate lazy var prePoint: CGPoint = .zero
    
    lazy var timer =  DispatchSource.makeTimerSource(flags: [], queue:DispatchQueue.main)
    
    // MARK: - Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        view.addSubview(imageView)
        
        view.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(AESViewController.handlePanGesture(pan:))))
    }
}

// MARK: - 拖拽(UIPanGestureRecognizer)手势代替轻扫(UISwipeGestureRecognizer)手势
enum UIPanGestureRecognizerDirection{
    case undefined,up,down,left,right
}

extension AESViewController {
    //加载手势
    func handlePanGesture(pan: UIPanGestureRecognizer){
        switch(pan.state){
            case .began:
                prePoint = pan.location(in: pan.view!)
            case .changed:
                let curP = pan.location(in: pan.view!)
                let isVerticalGesture = fabs(curP.y) > fabs(prePoint.y)   // 上下之分
                let isHorizontalGesture = fabs(curP.x) > fabs(prePoint.x) // 左右之分
                let isHelpDirection = fabs((curP.y - prePoint.y)/(curP.x - prePoint.x)) <= 1
                if isHorizontalGesture &&  isHelpDirection{
                    direction = .right
                } else if !isHorizontalGesture && isHelpDirection {
                    direction = .left
                } else if isVerticalGesture && !isHelpDirection {
                    direction = .down
                } else if !isVerticalGesture && !isHelpDirection{
                    direction = .up
                }else {
                    direction = .undefined
                }
                break
            case .ended:
                switch direction {
                    case .up:
                        handleUpwardsGesture()
                    case .down:
                        handleDownwardsGesture()
                    case .right:
                        handleRightwardsGesture()
                    case .left:
                        handleLeftwardsGesture()
                    default:
                        break
                }
            default:
                break
            
        }
    }
    
    fileprivate func handleUpwardsGesture() {
        print("Up")
    }
    
    fileprivate func handleDownwardsGesture() {
        print("Down")
    }
    fileprivate func handleRightwardsGesture() {
        print("Right")
    }
    fileprivate func handleLeftwardsGesture() {
        print("left")
    }

  • 手势一张UIImageView实现简单轮播

    • 扩展:

      • 使用DispatchSourceTimer定时器


cube图片轮播.gif

- ###越来越像了


💧.gif


  • 轻轻点击,关注我简书

轻轻点击,关注我简书

轻轻点击,关注我微博

浏览我的GitHub


  • 扫一扫,关注我

扫一扫,关注我.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容