我正在制作掉落的瓷砖游戏。当您点击(单个)时我已经实现了普通的图块,它将被删除。接下来,我想在同时点击2个图块时制作双图块,这些图块将被删除。但我不知道“你是否同时点击2个双层瓷砖”。
双层瓷砖从屏幕底部从屏幕顶部落下。如果瓷砖到达底部,则会从屏幕顶部显示新的瓷砖。 现在我可以通过两根手指同时检测单击并点击。
如何制作双层瓷砖?
更新:
到目前为止,我可以通过GestureRecognizer检测到2个手指触摸。但无法检测到“您触摸的是哪种观点”。即使您触摸了2个图块,“gesture.view”的日志也只是一个视图。我想做的是像下面的代码:
if gesture.view.count == 2 && gesture.view is GameTileDouble {
print("touched 2 doubleTiles at the same time")
}
Tiles Class
class GameTile: UIImageView {
init(named: String, frame: CGRect) {
super.init(frame: frame)
super.image = (UIImage(named: named))
super.isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
普通瓷砖等级
class GameTileNormal: GameTile {
let namedDefault: String
var frameDefault: CGRect
let isHiddenDefault: Bool
var isUserInteractionEnabledDefault: Bool
let colorName: UIColor
var tileLane: Int
init(
named: String,
frame: CGRect,
isHidden: Bool = false,
isUserInteractionEnabled: Bool = true,
color: UIColor = UIColor.blue,
lane: Int) {
namedDefault = named
isHiddenDefault = isHidden
frameDefault = frame
isUserInteractionEnabledDefault = isUserInteractionEnabled
colorName = color
tileLane = lane
super.init(named: named, frame: frame)
super.isHidden = isHiddenDefault
super.isUserInteractionEnabled = isUserInteractionEnabledDefault
super.backgroundColor = colorName
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Double Tile Class
class GameTileDouble: GameTile {
let namedDefault: String
var frameDefault: CGRect
let isHiddenDefault: Bool
let isUserInteractionEnabledDefault: Bool
let colorName: UIColor
var tileLane: Int
init(
named: String,
frame: CGRect,
isHidden: Bool = false,
isUserInteractionEnabled: Bool = true,
color: UIColor = UIColor.yellow,
lane: Int) {
namedDefault = named
frameDefault = frame
isHiddenDefault = isHidden
isUserInteractionEnabledDefault = isUserInteractionEnabled
colorName = color
tileLane = lane
super.init(named: named, frame: frame)
super.isHidden = isHiddenDefault
super.isUserInteractionEnabled = isUserInteractionEnabledDefault
super.backgroundColor = colorName
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let tileNormal = GameTileNormal.init(named: "normal.png",frame: CGRect(x: 0, y:0, width: 50, height: 50),isUserInteractionEnabled: true, lane: 1)
self.view.addSubview(tileNormal)
let tileDouble1 = GameTileDouble.init(named: "black.png",frame: CGRect(x:150, y: 0, width: 50, height: 50),isUserInteractionEnabled: true, lane: 2)
self.view.addSubview(tileDouble1)
let tileDouble2 = GameTileDouble.init(named: "black.png",frame: CGRect(x: 200, y: 0, width: 50, height: 50),isUserInteractionEnabled: true, lane: 3)
self.view.addSubview(tileDouble2)
//detect single tap
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(StandardGameViewController.handleSingleTapGesture(_:)))
singleTapGesture.numberOfTapsRequired = 1
singleTapGesture.numberOfTouchesRequired = 1
view.addGestureRecognizer(singleTapGesture)
//detect tap at the same time by 2 fingers
let doubleTapGesture = UITapGestureRecognizer(target: self , action: #selector(StandardGameViewController.handleDoubleTapGesture(_:)))
doubleTapGesture.numberOfTouchesRequired = 2
view.addGestureRecognizer(doubleTapGesture)
}
//something function to move tiles...
@objc func handleSingleTapGesture(_ gesture: UITapGestureRecognizer) {
print("single tap")
//if tapped tileNormal, the normalTile is deleted (removeFromSuperview)
}
@objc func handleDoubleTapGesture(_ gesture: UITapGestureRecognizer) {
print("tap at the same time by 2 fingers")
//if tapped doubleTile1 and tileDouble1 and tileDouble2, these tiles will be deleted (this is the point I don't have any idea
}
答案 0 :(得分:1)
您只需要在添加了图块的view
上启用多点触控
//Assuming it's on self.view
self.view.isMultipleTouchEnabled = true
注意:这样做会支持N
次触摸。
然后你只能使用handleSingleTapGesture:
无需handleDoubleTapGesture:
。
只需确保在每个图块上添加点按手势,然后将点按操作指定给handleSingleTapGesture:
,这样就可以为您点按的每个图块调用该图块。
然后只需删除这样的瓷砖:
gesture.view?.removeFromSuperview()
示例:
@IBAction func handleSingleTapGesture(_ gesture: UITapGestureRecognizer) {
guard let view = gesture.view else { return }
print("Current view tapped: ", terminator: "")
if view.isKind(of: GameTileNormal.self) {
print("Normal Tile")
}
else if view.isKind(of: GameTileDouble.self) {
print("Double Tile")
}
view.removeFromSuperview()
}