手勢

//
//  ViewController.swift
//  SwiftAppLab
//
//  Created by Bear on 2017/2/4.
//  Copyright ? 2017年 BeargerHunter. All rights reserved.
//

import UIKit
import Foundation
import SnapKit

class ViewController: UIViewController{
    
    lazy var box = {()->UIView in
        let box = UIView()
        box.backgroundColor = UIColor.red
        return box
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = UIColor.white
        self.title = "Bear"
        
        self.view.addSubview(box)
        box.snp.makeConstraints { (make) in
            make.width.height.equalTo(50)
            make.center.equalTo(self.view)
        }
        
        //tap
        let tapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(handleTapGesture(_:)))
        box.addGestureRecognizer(tapGestureRecognizer)
        //double tap
        let doubleTapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(handleDoubleTapGesture(_:)))
        doubleTapGestureRecognizer.numberOfTapsRequired = 2
        doubleTapGestureRecognizer.delegate = self
        box.addGestureRecognizer(doubleTapGestureRecognizer)
        
        //longpress
        let longPressGestureRecgonizer = UILongPressGestureRecognizer.init(target: self, action: #selector(handleLongPressGesture(_:)))
        box.addGestureRecognizer(longPressGestureRecgonizer)
        //swip
        let swipLeftGestureRecgonizer = UISwipeGestureRecognizer.init(target: self, action: #selector(handleSwipGesture(_:)))
        swipLeftGestureRecgonizer.direction = .left
        box.addGestureRecognizer(swipLeftGestureRecgonizer)
        
        let swipRightGestureRecgonizer = UISwipeGestureRecognizer.init(target: self, action: #selector(handleSwipGesture(_:)))
        swipRightGestureRecgonizer.direction = .right
        box.addGestureRecognizer(swipRightGestureRecgonizer)
        
        let swipUpGestureRecgonizer = UISwipeGestureRecognizer.init(target: self, action: #selector(handleSwipGesture(_:)))
        swipUpGestureRecgonizer.direction = .up
        box.addGestureRecognizer(swipUpGestureRecgonizer)
        
        let swipDownGestureRecgonizer = UISwipeGestureRecognizer.init(target: self, action: #selector(handleSwipGesture(_:)))
        swipDownGestureRecgonizer.direction = .down
        box.addGestureRecognizer(swipDownGestureRecgonizer)
        
        //pan
        let panGestureRecognizer = UIPanGestureRecognizer.init(target: self, action: #selector(handlePanGesture(_:)))
        box.addGestureRecognizer(panGestureRecognizer)
        
        //pinch
        let pinchGestureRecgonizer = UIPinchGestureRecognizer.init(target: self, action: #selector(handlePinchGesture(_:)))
        pinchGestureRecgonizer.delegate = self
        box.addGestureRecognizer(pinchGestureRecgonizer)
        
        //rotation:
        let rotationGestureRecgonizer = UIRotationGestureRecognizer.init(target: self, action: #selector(handleRotationGesture(_:)))
        rotationGestureRecgonizer.delegate = self
        box.addGestureRecognizer(rotationGestureRecgonizer)
        //MARK: 如果需要同時旋轉(zhuǎn)和縮放需要設(shè)置代碼方法返回true
    }
    
    var rate:CGFloat = 100.0
    func handleTapGesture(_ gesture:UIGestureRecognizer) {
        
        
        UIView.animate(withDuration: 2, animations: {
            self.box.frame = CGRect.init(x: self.box.frame.origin.x, y: self.box.frame.origin.y, width: self.rate, height: self.rate)
            self.rate += 100.0
            
        })
    }
    
    func handleDoubleTapGesture(_ gesture:UIGestureRecognizer) {
        UIView.animate(withDuration: 2, animations: {
            self.box.frame = CGRect.init(x: self.box.frame.origin.x, y: self.box.frame.origin.y, width: 50, height: 50)
            
        })
    }
    
    func handleLongPressGesture(_ gesture:UIGestureRecognizer) {
        UIView.animate(withDuration: 2) { 
            self.box.backgroundColor = UIColor.gray
        }
    }
    
    func handleSwipGesture(_ gesture:UISwipeGestureRecognizer) {
        
        switch gesture.direction {
        case UISwipeGestureRecognizerDirection.left:
            UIView.animate(withDuration: 2, animations: {
                self.box.frame = CGRect.init(x: self.box.frame.origin.x-100, y: self.box.frame.origin.y, width: 50, height: 50)
                
            })
        case UISwipeGestureRecognizerDirection.right:
            UIView.animate(withDuration: 2, animations: {
                self.box.frame = CGRect.init(x: self.box.frame.origin.x+100, y: self.box.frame.origin.y, width: 50, height: 50)
                
            })
        case UISwipeGestureRecognizerDirection.up:
            UIView.animate(withDuration: 2, animations: {
                self.box.frame = CGRect.init(x: self.box.frame.origin.x, y: self.box.frame.origin.y-100, width: 50, height: 50)
                
            })
        case UISwipeGestureRecognizerDirection.down:
            UIView.animate(withDuration: 2, animations: {
                self.box.frame = CGRect.init(x: self.box.frame.origin.x, y: self.box.frame.origin.y+100, width: 50, height: 50)
                
            })
        default:
            break
        }
        
    }
    
    func handlePanGesture(_ gesture:UIPanGestureRecognizer) {
        
        let transP = gesture.translation(in: box)
        self.box.transform = self.box.transform.translatedBy(x: transP.x, y: transP.y)
        gesture.setTranslation(CGPoint.zero, in: self.box)
    }
    
    func handlePinchGesture(_ gesture:UIPinchGestureRecognizer) {
        self.box.transform = self.box.transform.scaledBy(x: gesture.scale, y: gesture.scale)
        gesture.scale = 1.0
    }
    
    func handleRotationGesture(_ gesture:UIRotationGestureRecognizer) {
        self.box.transform = self.box.transform.rotated(by: gesture.rotation)
        gesture.rotation = 0
    }
}

extension ViewController :UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive press: UIPress) -> Bool {
        return true
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        return true
    }
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末棕诵,一起剝皮案震驚了整個濱河市裁良,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌校套,老刑警劉巖价脾,帶你破解...
    沈念sama閱讀 206,311評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異笛匙,居然都是意外死亡侨把,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,339評論 2 382
  • 文/潘曉璐 我一進(jìn)店門妹孙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來秋柄,“玉大人,你說我怎么就攤上這事蠢正『П剩” “怎么了?”我有些...
    開封第一講書人閱讀 152,671評論 0 342
  • 文/不壞的土叔 我叫張陵嚣崭,是天一觀的道長笨触。 經(jīng)常有香客問我,道長雹舀,這世上最難降的妖魔是什么芦劣? 我笑而不...
    開封第一講書人閱讀 55,252評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮说榆,結(jié)果婚禮上持寄,老公的妹妹穿的比我還像新娘源梭。我一直安慰自己,他們只是感情好稍味,可當(dāng)我...
    茶點故事閱讀 64,253評論 5 371
  • 文/花漫 我一把揭開白布废麻。 她就那樣靜靜地躺著,像睡著了一般模庐。 火紅的嫁衣襯著肌膚如雪烛愧。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,031評論 1 285
  • 那天掂碱,我揣著相機(jī)與錄音怜姿,去河邊找鬼。 笑死疼燥,一個胖子當(dāng)著我的面吹牛沧卢,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播醉者,決...
    沈念sama閱讀 38,340評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼但狭,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了撬即?” 一聲冷哼從身側(cè)響起立磁,我...
    開封第一講書人閱讀 36,973評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎剥槐,沒想到半個月后唱歧,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,466評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡粒竖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,937評論 2 323
  • 正文 我和宋清朗相戀三年颅崩,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蕊苗。...
    茶點故事閱讀 38,039評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡沿后,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出岁歉,到底是詐尸還是另有隱情得运,我是刑警寧澤,帶...
    沈念sama閱讀 33,701評論 4 323
  • 正文 年R本政府宣布锅移,位于F島的核電站熔掺,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏非剃。R本人自食惡果不足惜置逻,卻給世界環(huán)境...
    茶點故事閱讀 39,254評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望备绽。 院中可真熱鬧券坞,春花似錦鬓催、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,259評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至猴伶,卻和暖如春课舍,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背他挎。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評論 1 262
  • 我被黑心中介騙來泰國打工筝尾, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人办桨。 一個月前我還...
    沈念sama閱讀 45,497評論 2 354
  • 正文 我出身青樓筹淫,卻偏偏與公主長得像,于是被迫代替她去往敵國和親呢撞。 傳聞我的和親對象是個殘疾皇子损姜,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,786評論 2 345

推薦閱讀更多精彩內(nèi)容