• Swift之手势总结


    手势个人笔记,代码简单,不过多赘述。

    建立手势只需三步

    • 建立和设置手势识别器实例,设置target,action,指定一些属性
    • 附加识别器到视图
    • 实现action方法

    1、首先还是创建一个view

    @IBOutlet var view1: UIView!

    2、实现手势绑定

    //-----------------------手势---------------------------
    /**点击手势*/
    func tapGestureDemo() {
        //建立手势识别器
        let gesture = UITapGestureRecognizer(target: self, action: "viewTap:")
        //附加识别器到视图
        self.view1.addGestureRecognizer(gesture)
    }
    
    /**缩放手势*/
    func pinchGestureDemo() {
        //建立手势识别器
        let gesture = UIPinchGestureRecognizer(target: self, action: "viewPinch:")
        //附加识别器到视图
        self.view1.addGestureRecognizer(gesture)
    }
    
    /**旋转手势*/
    func rotationGestureDemo() {
        //建立手势识别器
        let gesture = UIRotationGestureRecognizer(target: self, action: "viewRotation:")
        //附加识别器到视图
        self.view1.addGestureRecognizer(gesture)
    }
    
    /**滑动手势*/
    func swipeGestureDemo() {
        //建立手势识别器
        let gesture = UISwipeGestureRecognizer(target: self, action: "viewSwipe:")
        //附加识别器到视图
        self.view1.addGestureRecognizer(gesture)
    }
    
    /**平移手势*/
    func panGestureDemo() {
        //建立手势识别器
        let gesture = UIPanGestureRecognizer(target: self, action: "viewPan:")
        //附加识别器到视图
        self.view1.addGestureRecognizer(gesture)
    }
    
    /**长按手势*/
    func LongPressGestureDemo() {
        //建立手势识别器
        let gesture = UILongPressGestureRecognizer(target: self, action: "viewLongPress:")
        //附加识别器到视图
        self.view1.addGestureRecognizer(gesture)
    }
    
    /**自定义手势*/
    func custonGestureDemo() {
        //建立手势识别器
        let gesture = CustomGestureRecognizer(target: self, action: "viewCustom:")
        //附加识别器到视图
        self.view1.addGestureRecognizer(gesture)
    }
    
    //-----------------------手势action事件---------------------------
    /**点击事件*/
    @IBAction func viewTap(sender: UITapGestureRecognizer) {
        print("clicked...")
    }
    
    /**缩放事件*/
    func viewPinch(sender: UIPinchGestureRecognizer) {
        print("pinch...")
        let _width = view1.bounds.width
        let _height = view1.bounds.height
        view1.bounds.size = CGSize( _width * sender.scale, height: _height * sender.scale)
    }
    
    /**旋转事件*/
    func viewRotation(sender: UIRotationGestureRecognizer) {
        view1.transform = CGAffineTransformMakeRotation(sender.rotation)
    }
    
    /**滑动事件*/
    var offsetX: CGFloat = 0
    func viewSwipe(sender: UISwipeGestureRecognizer) {
        if sender.direction == UISwipeGestureRecognizerDirection.Right {
            //每次向右平移20
            offsetX += 20
            view1.transform = CGAffineTransformMakeTranslation(offsetX, 0)
        }
    }
    
    /**平移事件*/
    func viewPan(sender: UIPanGestureRecognizer) {
        let _transX = sender.translationInView(view1).x
        let _transY = sender.translationInView(view1).y
    
        view1.transform = CGAffineTransformMakeTranslation(_transX, _transY)
    }
    
    /**长按事件*/
    func viewLongPress(sender: UILongPressGestureRecognizer) {
        print("long pressed....")
    }
    
    func viewCustom(sender: CustomGestureRecognizer) {
        print("custom gesture...")
    }

    3、最后自定义手势,很简单一个例子,可自由扩展

    • 新建一个CustomGestureRecognizer自定义手势类,继承自UIGestureRecognizer
    • 切记,引入UIGestureRecognizerSubclass
    • 开始实现类容,下面代码实现从左上角滑动到右下角实例
    import UIKit
    import UIKit.UIGestureRecognizerSubclass
    
    class CustomGestureRecognizer: UIGestureRecognizer {
    var leftTop = false
    var rightDown = false
    
    override init(target: AnyObject?, action: Selector) {
        super.init(target: target, action: action)
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
    
    }
    
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) {
        let myTouch = touches.first! as UITouch
        let myLocation = myTouch.locationInView(self.view)
        if myLocation.x < 10 && myLocation.y < 10 {
            leftTop = true
        }
    
        if (myLocation.x + 10) > self.view?.bounds.width && (myLocation.y + 10) > self.view?.bounds.height {
            rightDown = true
        }
    
        if leftTop && rightDown {
            self.state = UIGestureRecognizerState.Ended
        }
    
    }
    
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent) {
        self.reset()
    }
    }



    文/疯狂的米老鼠(简书作者)
    原文链接:http://www.jianshu.com/p/6ef6603d035d
    著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
  • 相关阅读:
    C#添加修改删除文件文件夹大全
    实用且不花哨的js代码大全
    vs2005 2008快捷键
    C#:String.Format数字格式化输出
    获取农历日期
    Vim 常用快捷键
    一个简单的makefile示例及其注释
    nginx源码剖析(1)概要
    利用Vim 打造开发环境(一)>Linux 字符界面 vim的配置
    Ubuntu 9.10设置摘要
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6248848.html
Copyright © 2020-2023  润新知