1、点击手势
2、拖动手势
3、长按手势
4、滑动手势
5、捏合手势
6、旋转手势
完整代码
import UIKit
class ViewController: UIViewController
{
var imageView: UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
imageView.center = self.view.center
imageView.image = #imageLiteral(resourceName: "arrows")
imageView.isUserInteractionEnabled = true
self.view.addSubview(imageView)
// MARK: 拖动手势
let pan = UIPanGestureRecognizer(target: self, action: #selector(ViewController.didPan(pan:)))
pan.maximumNumberOfTouches = 1
imageView.addGestureRecognizer(pan)
// MARK: 长按手势
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPress(longPress:)))
imageView.addGestureRecognizer(longPress)
// MARK: 点击手势
// 单击
let tapOne = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapOne))
imageView.addGestureRecognizer(tapOne)
// 双击
let tapDouble = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapDouble))
tapDouble.numberOfTapsRequired = 2
imageView.addGestureRecognizer(tapDouble)
// 声明单击事件需要双击事件检测失败后才会执行
tapOne.require(toFail: tapDouble)
// MARK: 滑动手势
// 上滑
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swipeGesture(swipe:)))
swipeUp.direction = .up
self.view.addGestureRecognizer(swipeUp)
// 下滑
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swipeGesture(swipe:)))
swipeDown.direction = .down
self.view.addGestureRecognizer(swipeDown)
// 左滑
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swipeGesture(swipe:)))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
// 右滑
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swipeGesture(swipe:)))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
// MARK: 捏合手势
let pinch = UIPinchGestureRecognizer(target: self, action: #selector(ViewController.didPinch(pinch:)))
imageView.addGestureRecognizer(pinch)
// MARK: 旋转手势
let rotation = UIRotationGestureRecognizer(target: self, action: #selector(ViewController.didRotation(rotation:)))
imageView.addGestureRecognizer(rotation)
}
func didRotation(rotation: UIRotationGestureRecognizer)
{
print("旋转的角度:\(rotation.rotation*(180/(CGFloat(Double.pi))))")
imageView.transform = CGAffineTransform(rotationAngle: rotation.rotation)
}
func didPinch(pinch: UIPinchGestureRecognizer)
{
print("捏合比例:\(pinch.scale)")
print("捏合速度:\(pinch.velocity)")
imageView.transform = CGAffineTransform(scaleX: pinch.scale, y: pinch.scale)
}
func swipeGesture(swipe: UISwipeGestureRecognizer)
{
switch swipe.direction
{
case UISwipeGestureRecognizerDirection.up:
print("向上滑动")
case UISwipeGestureRecognizerDirection.down:
print("向下滑动")
case UISwipeGestureRecognizerDirection.left:
print("向左滑动")
case UISwipeGestureRecognizerDirection.right:
print("向右滑动")
default:
print("不明滑动")
}
}
func tapOne()
{
print("单击")
}
func tapDouble()
{
print("双击")
}
func longPress(longPress: UILongPressGestureRecognizer)
{
if longPress.state == .began
{
print("长按开始")
}
else
{
print("长按结束")
}
}
func didPan(pan: UIPanGestureRecognizer)
{
let point = pan.location(in: self.view)
imageView.center = point
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
网友评论