手势
2017-03-25 本文已影响18人
Bearger
//
// 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
}
}