//
// 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)系作者
- 文/潘曉璐 我一進(jìn)店門妹孙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來秋柄,“玉大人,你說我怎么就攤上這事蠢正『П剩” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵嚣崭,是天一觀的道長笨触。 經(jīng)常有香客問我,道長雹舀,這世上最難降的妖魔是什么芦劣? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮说榆,結(jié)果婚禮上持寄,老公的妹妹穿的比我還像新娘源梭。我一直安慰自己,他們只是感情好稍味,可當(dāng)我...
- 文/花漫 我一把揭開白布废麻。 她就那樣靜靜地躺著,像睡著了一般模庐。 火紅的嫁衣襯著肌膚如雪烛愧。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼但狭,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了撬即?” 一聲冷哼從身側(cè)響起立磁,我...
- 正文 年R本政府宣布锅移,位于F島的核電站熔掺,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏非剃。R本人自食惡果不足惜置逻,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望备绽。 院中可真熱鬧券坞,春花似錦鬓催、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至猴伶,卻和暖如春课舍,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背他挎。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- iOS手勢識別的詳細(xì)使用(拖動,縮放,旋轉(zhuǎn),點擊,手勢依賴,自定義手勢)
- UIPinchGestureRecognizer捏合手勢 alloc initWithTarget imageVi...
- 1薛匪、UIGestureRecognizer介紹 手勢識別在iOS上非常重要捐川,手勢操作移動設(shè)備的重要特征脓鹃,極大的增加...