【发布时间】:2018-04-24 16:04:28
【问题描述】:
我想要一个能够检测您的手指何时触摸的应用。如果应用程序检测到屏幕被点击,我想在那个地方放置一个 UIImage。这个应用程序需要有多个用户。到目前为止我有这个:
var users = 0
@IBOutlet weak var Person_1: UIImageView!
@IBOutlet weak var Person_2: UIImageView!
@IBOutlet weak var Person_3: UIImageView!
@IBOutlet weak var Person_4: UIImageView!
@IBOutlet weak var Person_5: UIImageView!
var fingers = [String?](repeating: nil, count:5)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
for touch in touches{
let point = touch.location(in: self.view)
for (index,finger) in fingers.enumerated() {
if finger == nil {
fingers[index] = String(format: "%p", touch)
print("finger \(index+1): x= \(point.x) , y= \(point.y)")
if index == 0 {
Person_1.center = CGPoint(x: point.x , y: point.y)
Person_1.isHidden = false
users = 1
}
if index == 1 {
Person_2.center = CGPoint(x: point.x , y: point.y)
Person_2.isHidden = false
users = 2
}
if index == 2 {
Person_3.center = CGPoint(x: point.x , y: point.y)
Person_3.isHidden = false
users = 3
}
if index == 3 {
Person_4.center = CGPoint(x: point.x , y: point.y)
Person_4.isHidden = false
users = 4
}
if index == 4 {
Person_5.center = CGPoint(x: point.x , y: point.y)
Person_5.isHidden = false
users = 5
}
print("Users: \(users)")
break
}
}
}
check_users()
}
func check_users(){
if users == 5 {
let timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.choosewinner4), userInfo: nil, repeats: false)
}
if users == 4 {
let timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.choosewinner3), userInfo: nil, repeats: false)
}
if users == 3 {
let timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.choosewinner2), userInfo: nil, repeats: false)
}
if users == 2 {
let timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.choosewinner1), userInfo: nil, repeats: false)
}
}
@objc func choosewinner1(){
let diceRoll1 = Int(arc4random_uniform(2) + 1)
print(diceRoll1)
if diceRoll1 == 1 {
Person_1.isHidden = true
}
if diceRoll1 == 2{
Person_2.isHidden = true
}
}
每当新用户触摸屏幕时,users Int 将在 users Int 上计算 1 个用户。我有一个函数可以检查当你有 x 数量的用户时它需要做什么,称为“check_users()”。这段代码的问题是,如果你有 2 个人触摸屏幕,用户 Int 将是 2。如果你有 3 人触摸屏幕,用户 Int 将是第一个 2,然后通过 check_users() 函数,然后通过用户 Int = 3 的 check_users() 函数。我希望应用程序等待几秒钟,然后通过最终用户 Int 值的 users_check() 函数。有人知道怎么做吗?
【问题讨论】:
标签: swift touch touchesbegan