常用的手势识别器
let tap : UITapGestureRecognizer //轻拍
let pin : UIPinchGestureRecognizer //捏
let swipe : UISwipeGestureRecognizer //滑动
let rotate : UIRotationGestureRecognizer //旋转
let pan : UIPanGestureRecognizer //移动
let longpress : UILongPressGestureRecognizer //长按
轻拍
let tap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(GameGesture.tap))
self.view?.addGestureRecognizer(tap)
捏
let pin : UIPinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(GameGesture.pin(recognizer:)))
self.view?.addGestureRecognizer(pin)
滑动
//默认是右滑
let swpieRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameGesture.swipe(recognizer:)))
let swpieLeft : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameGesture.swipe(recognizer:)))
let swpieUp : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameGesture.swipe(recognizer:)))
let swpieDown : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameGesture.swipe(recognizer:)))
swpieRight.direction = .right
swpieLeft.direction = .left
swpieUp.direction = .up
swpieDown.direction = .down
self.view?.addGestureRecognizer(swpieRight)
self.view?.addGestureRecognizer(swpieLeft)
self.view?.addGestureRecognizer(swpieUp)
self.view?.addGestureRecognizer(swpieDown)
旋转
let rotate : UIRotationGestureRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(GameGesture.rotate(recognizer:)))
self.view?.addGestureRecognizer(rotate)
移动
let pan : UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(GameGesture.pan(recognizer:)))
self.view?.addGestureRecognizer(pan)
长按
let longPress : UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(GameGesture.longpress(recognizer:)))
self.view?.addGestureRecognizer(longPress)
初始化
override func didMove(to view: SKView) {
self.size = UIScreen.main.bounds.size
node = SKSpriteNode()
node.name = "gesture"
node.color = UIColor.red
node.size = CGSize(width: 100, height: 100)
node.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
self.addChild(node)
//添加手势
....
}
事件方法
@objc func tap() {
node.isHidden = !node.isHidden
}
var lastScale : CGFloat = 0.0
@objc func pin(recognizer : UIPinchGestureRecognizer) {
self.view?.bringSubview(toFront: view!)
if recognizer.state == UIGestureRecognizerState.ended {
lastScale = 1.0 //放手之后比例设置为1:1
return
}
//上一次的缩放比例 - 当前捏缩放比例 = 下一次比例的变化值.
let scale : CGFloat = 1.0 - (lastScale - recognizer.scale)
recognizer.view?.transform = recognizer.view!.transform.scaledBy(x: scale, y: scale)
lastScale = recognizer.scale
}
@objc func rotate(recognizer : UIRotationGestureRecognizer) {
self.node.zRotation = -recognizer.rotation
}
@objc func pan(recognizer : UIPanGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.changed {
let point = recognizer.location(in: self.view!)
self.node.position = CGPoint(x: point.x, y: point.y)
//这里考虑recognizer 和 node 的坐标系Y轴是相反的.
//所以,这里有一个bug,就是平移的时候,Y轴的走向是相反的.这个bug待解决.
}
}
@objc func swipe(recognizer : UISwipeGestureRecognizer) {
switch recognizer.direction {
case .left:
self.node.run(SKAction.moveBy(x: -100, y: 0, duration: 1))
break
case .right:
self.node.run(SKAction.moveBy(x: 100, y: 0, duration: 1))
break
case .up:
self.node.run(SKAction.moveBy(x: 0, y: 100, duration: 1))
break
case .down:
self.node.run(SKAction.moveBy(x: 0, y: -100, duration: 1))
break
default:break;
}
}
@objc func longpress(recognizer : UIRotationGestureRecognizer) {
self.node.color = UIColor.green
}