Swift 3 Gesture

//
//  GestureVC.swift
//  Gesture
//
//  Created by chenr on 2016/10/27.
//  Copyright © 2016年 SiCNU. All rights reserved.
//

//
//  ViewController.swift
//  Gesture
//
//  Created by chenr on 2016/10/27.
//  Copyright © 2016年 SiCNU. All rights reserved.
//

import UIKit

class GestureVC: UIViewController, UIGestureRecognizerDelegate {
    
    @IBOutlet weak var view1: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        tapGestureDemo()
        pinchGestureDemo()
        rotationGestureDemo()
        swipeGestureDemo()
        panGestureDemo()
        LongPressGestureDemo()
        custonGestureDemo()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //-----------------------手势---------------------------
    /**点击手势*/
    func tapGestureDemo() {
        //建立手势识别器
        let gesture = UITapGestureRecognizer(target: self, action: #selector(viewTap(_:)))
        //附加识别器到视图
        self.view1.addGestureRecognizer(gesture)
    }
    
    /**缩放手势*/
    func pinchGestureDemo() {
        //建立手势识别器
        let gesture = UIPinchGestureRecognizer(target: self, action: #selector(viewPinch(_:)))
        //附加识别器到视图
        self.view1.addGestureRecognizer(gesture)
    }
    
    /**旋转手势*/
    func rotationGestureDemo() {
        //建立手势识别器
        let gesture = UIRotationGestureRecognizer(target: self, action: #selector(viewRotation(_:)))
        //附加识别器到视图
        self.view1.addGestureRecognizer(gesture)
    }
    
    /**滑动手势*/
    func swipeGestureDemo() {
        //建立手势识别器
        let gesture = UISwipeGestureRecognizer(target: self, action: #selector(viewSwipe(_:)))
        //附加识别器到视图
        self.view1.addGestureRecognizer(gesture)
    }
    
    /**平移手势*/
    func panGestureDemo() {
        //建立手势识别器
        let gesture = UIPanGestureRecognizer(target: self, action: #selector(viewPan(_:)))
        //附加识别器到视图
        self.view1.addGestureRecognizer(gesture)
    }
    
    /**长按手势*/
    func LongPressGestureDemo() {
        //建立手势识别器
        let gesture = UILongPressGestureRecognizer(target: self, action: #selector(viewLongPress(_:)))
        //附加识别器到视图
        self.view1.addGestureRecognizer(gesture)
    }
    
    /**自定义手势*/
    func custonGestureDemo() {
        //建立手势识别器
        let gesture = CustomGestureRecognizer(target: self, action: #selector(viewCustom(_:)))
        //附加识别器到视图
        self.view1.addGestureRecognizer(gesture)
    }
    
    //-----------------------手势action事件---------------------------
    /**点击事件*/
    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: _width * sender.scale, height: _height * sender.scale)
    }
    
    /**旋转事件*/
    func viewRotation(_ sender: UIRotationGestureRecognizer) {
        print("rotation ....")
        
        view1.transform = CGAffineTransform(rotationAngle: sender.rotation)
    }
    
    /**滑动事件*/
    var offsetX: CGFloat = 0
    func viewSwipe(_ sender: UISwipeGestureRecognizer) {
        print("swipe ....")
        
        if sender.direction == UISwipeGestureRecognizerDirection.right {
            //每次向右平移20
            offsetX += 20
            view1.transform = CGAffineTransform(translationX: offsetX, y: 0)
        }
    }
    
    /**平移事件*/
    func viewPan(_ sender: UIPanGestureRecognizer) {
        print("pan ....")

        let _transX = sender.translation(in: view1).x
        let _transY = sender.translation(in: view1).y
        
        view1.transform = CGAffineTransform(translationX: _transX, y: _transY)
    }
    
    /**长按事件*/
    func viewLongPress(_ sender: UILongPressGestureRecognizer) {
        print("long pressed....")
    }
    
    func viewCustom(_ sender: CustomGestureRecognizer) {
        print("custom gesture...")
    }    
}


Comments are closed.