我已经能够检测到用户是在触摸屏幕的左侧还是右侧,然后执行某个功能。我正在尝试检测屏幕的两侧是否同时被触摸,如果是则执行另一个功能。
这就是我所拥有的,当同时触摸屏幕的两侧时,它不起作用。
这是在更新函数中。
if (touched) {
var isRight : Bool = false
var isLeft : Bool = false
if(location.x < 0){
isLeft = true
moveLeft()
}
else if(location.x > 0){
isRight = true
moveRight()
}
else if (isRight && isLeft){
moveUp()
}
}
最佳答案
以下检查触摸端的所有触摸并构建一个联合值作为整数值,然后可以使用 switch 语句检查:
struct Sides : OptionSet {
let rawValue: Int
static let left = Sides(rawValue:1)
static let right = Sides(rawValue:2)
static let both : Sides = [.left, .right]
static let none : Sides = []
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// Get sides touched
let touched = touches.map { return $0.location(in: view) }.reduce(Sides.none) {
if $1.x < 0 {
return [ $0, Sides.left ]
}
else if $1.x > 0 {
return [ $0, Sides.right ]
}
else {
return $0
}
}
switch(touched) {
case Sides.left:
// handle left
break
case Sides.right:
// handle right
break
case Sides.both:
// handle both
break
case Sides.none:
fallthrough
default:
// none
break
}
}
关于ios - Swift 同时检测屏幕左右两侧的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42519639/