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

作者: Mg明明就是你 | 来源:发表于2017-05-24 13:56 被阅读299次
  • 拖拽(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

相关文章

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

    拖拽(UIPanGestureRecognizer)手势代替轻扫(UISwipeGestureRecognizer...

  • 手势

    点击手势 捏合手势 旋转手势 轻扫手势 拖拽手势 边缘平移手势 长按手势

  • IOS 中的手势

    IOS中有六种手势,分别是点击、长按、轻扫、选转、捏合、拖拽。 /************************...

  • iOS手势应用

    常用手势包括点按、轻扫、长按、捏合、旋转、拖拽手势等等 注意点: 对图片添加手势要开启用户交互 可以对图片添加多个...

  • iOS七种手势详解

    1、轻拍手势 2、捏合手势 3、旋转手势 4、平移手势 5、边缘轻扫手势 6、长按手势 7、轻扫手势 给image...

  • iOS开发中六种手势识别

    轻击手势(TapGestureRecognizer) 轻扫手势 (SwipeGestureRecognizer) ...

  • 20.iOS手势识别

    轻击手势(TapGestureRecognizer)轻扫手势 (SwipeGestureRecognizer)长按...

  • 02.3--iOS手势识别

    一、手势识别(点按、长按、轻扫) 1、点击手势在控制器view上添加一个iamgeView 2、长按手势 3、轻扫...

  • UI梳理——手势

    手势分类: 手势的创建: 方法的实现: 轻扫手势:UISwipeGestureRecognizer 长按手势: 以...

  • IOS15之轻扫手势

    IOS15之轻扫手势 ios中的手势,也是重点内容,必须学会使用。常见的有7大手势,今天介绍轻扫手势 给table...

网友评论

    本文标题:MG--Swift3.0拖拽手势代替轻扫

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