假设我有一张树的图像,当我点击屏幕时,它会变成汽车的图像,我该怎么做?这是我到目前为止的代码,但它不起作用。 (我有三张图片名为" gp1"," gp2"和" gp3"所有这些图片都有不同数量的点按来更改图片。 )
我在Spritekit中并使用Swift。
class GameScene: SKScene, SKPhysicsContactDelegate {
//Create Touch
createTouch()
}
override func didMoveToView(view: SKView) {
func createTouch() {
gestureRecognizer.numberOfTapsRequired = 1
gestureRecognizer2.numberOfTapsRequired = 2
gestureRecognizer3.numberOfTapsRequired = 3
gestureRecognizer == UITapGestureRecognizer(target: self, action: "gp2")
gestureRecognizer2 == UITapGestureRecognizer(target: self, action: "gp3")
gestureRecognizer3 == UITapGestureRecognizer(target: self, action: "gp1")
}
}
答案 0 :(得分:0)
正如Mundi所说,对于每个UIImageView,请确保userInteractionEnabled = true。否则,用户无法点击图像。然后,创建gestureRecognizer。将操作设置为将处理手势的方法。接下来,将gestureRecognizer添加到UIImageView。最后,编写处理手势的方法。
@IBOutlet var gp1: UIImageView!
@IBOutlet var gp2: UIImageView!
@IBOutlet var gp3: UIImageView!
func createTouch() {
// maker sure userInteractionEnabled is true
gp1.userInteractionEnabled = true;
gp2.userInteractionEnabled = true;
gp3.userInteractionEnabled = true;
// create the gesture recognizers
let gestureRecognizer1 = UITapGestureRecognizer(target: self, action: "tapGesture:")
let gestureRecognizer2 = UITapGestureRecognizer(target: self, action: "tapGesture:")
let gestureRecognizer3 = UITapGestureRecognizer(target: self, action: "tapGesture:")
// set the number of taps
gestureRecognizer1.numberOfTapsRequired = 1
gestureRecognizer2.numberOfTapsRequired = 2
gestureRecognizer3.numberOfTapsRequired = 3
// add the gesture recognizer to the UIImageViews
gp1.addGestureRecognizer(gestureRecognizer1)
gp2.addGestureRecognizer(gestureRecognizer2)
gp3.addGestureRecognizer(gestureRecognizer3)
}
由于这些都是轻击手势,我决定使用一种方法和一种开关语句。但是,如果您愿意,可以为每个手势创建不同的方法。
func tapGesture(gesture: UITapGestureRecognizer) {
switch gesture.numberOfTapsRequired {
case 1:
gp1.image = UIImage(named: "car")
case 2:
gp2.image = UIImage(named: "car")
case 3:
gp3.image = UIImage(named: "car")
default:
break
}
}
答案 1 :(得分:-1)
您需要将识别器添加到视图中并实现操作处理程序gp2
等。
对于UIImageView
,您还应确保将userInteractionEnabled
设置为true。