我的代码应该打印出当前记录的点击次数,它确实开始了,但随后随机停止了。有关解决此问题的任何提示?
我认为它与动画有关,但我不确定是什么原因导致应用停止响应。
import UIKit
class ViewController: UIViewController {
var taps = 0 // tracks number of screen taps
override func viewDidLoad() {
super.viewDidLoad()
// get screen dimensions (in points) for current device
let screenBounds = UIScreen.main.bounds
let screenWidth = screenBounds.width
let screenHeight = screenBounds.height
// create rect
let rect = CGRect(x: 0, y: screenHeight, width: screenWidth, height: screenHeight)
let view = UIView(frame: rect)
view.backgroundColor = .red
self.view.addSubview(view)
// rising animation
UIView.animate(withDuration: 5.0, delay: 0.0, options: .curveEaseInOut, animations: {
view.frame.origin.y = CGFloat(0)
}, completion: { finished in
})
}
// when user taps screen
@IBAction func screenTapped(_ sender: Any) {
taps += 1 // increment number of taps
print(taps)
}
}
最佳答案
试试这个
import UIKit
class ViewController: UIViewController {
var taps = 0 // tracks number of screen taps
override func viewDidLoad() {
super.viewDidLoad()
// get screen dimensions (in points) for current device
let screenBounds = UIScreen.main.bounds
let screenWidth = screenBounds.width
let screenHeight = screenBounds.height
// create rect
let rect = CGRect(x: 0, y: screenHeight, width: screenWidth, height: screenHeight)
let view = UIView(frame: rect)
view.backgroundColor = .red
self.view.addSubview(view)
//Add Tap gesture
let tap = UITapGestureRecognizer(target: self, action: #selector(screenTapped))
self.view.addGestureRecognizer(tap)
// rising animation
UIView.animate(withDuration: 5.0, delay: 0.0, options: .curveEaseInOut, animations: {
view.frame.origin.y = CGFloat(0)
}, completion: { finished in
})
}
// when user taps screen
@objc func screenTapped() {
taps += 1 // increment number of taps
print(taps)
}
}
关于ios - 为什么我的 UI 按钮在经过一定时间后停止响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57488269/