//
// 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: 如果需要同时旋转和缩放需要设置代码方法返回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
}
}
手势
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- UIPinchGestureRecognizer捏合手势 alloc initWithTarget imageVi...
- 1、UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加...