swift - touchesBegan 功能的计时器
问题描述
我想要一个应用程序来检测你的手指何时触摸。如果应用程序检测到屏幕被点击,我想在那个地方放置一个 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)
...
}
推荐阅读
- php - 在 Laravel 控制器或模型中自动加载模块或库
- javascript - 获取元素数据属性并将它们插入到按钮中
- c# - 如何提供存储在 FTP 空间中的文件
- php - 尝试使用 PHP 保存 csv 文件需要很长时间
- flutter - 当使用flutter web部署的页面没有打开文件上传时
- azure-functions - 如何使用 Azure 数据工厂生成动态批处理文件并执行它
- css - 意外
垂直滚动条时出现线条(当高度超过 100% 时) - reactjs - 为什么反应重新渲染另一个变量?
- dataframe - 在一列中重新组织数据集
- node.js - AWS Lambda 能否从位于 lambda 函数源代码中的 /tmp 文件夹向前端 UI 提供静态 .png 图标