touchesBegan 函数的定时器
Timer for touchesBegan function
我想要一个可以检测手指何时触摸的应用程序。如果应用程序检测到屏幕被点击,我想在那个地方放置一个 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
}
}
每当有新用户触摸屏幕时,用户Int 将在用户Int 上计为1 个用户。我有一个函数,当你有 x 个用户时,它会检查它需要做什么,叫做 "check_users()"。这段代码的问题是,如果你用 2 个人触摸屏幕,用户 Int 将为 2。如果你用 3 个人触摸屏幕,用户 Int 将首先为 2 并通过 check_users( ) 函数,然后使用用户 Int = 3 通过 check_users() 函数。我希望应用在使用最终用户 Int 值通过 users_check() 函数之前等待几秒钟。有人知道怎么做吗?
您可以使用延迟执行选择器的方法。要在延迟后调用方法,请使用:
perform(#selector(check_users), with: nil, afterDelay: 1)
要取消之前未完成的任何请求:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
NSObject.cancelPreviousPerformRequests(withTarget: self)
...
}
我想要一个可以检测手指何时触摸的应用程序。如果应用程序检测到屏幕被点击,我想在那个地方放置一个 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
}
}
每当有新用户触摸屏幕时,用户Int 将在用户Int 上计为1 个用户。我有一个函数,当你有 x 个用户时,它会检查它需要做什么,叫做 "check_users()"。这段代码的问题是,如果你用 2 个人触摸屏幕,用户 Int 将为 2。如果你用 3 个人触摸屏幕,用户 Int 将首先为 2 并通过 check_users( ) 函数,然后使用用户 Int = 3 通过 check_users() 函数。我希望应用在使用最终用户 Int 值通过 users_check() 函数之前等待几秒钟。有人知道怎么做吗?
您可以使用延迟执行选择器的方法。要在延迟后调用方法,请使用:
perform(#selector(check_users), with: nil, afterDelay: 1)
要取消之前未完成的任何请求:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
NSObject.cancelPreviousPerformRequests(withTarget: self)
...
}