ios - 为什么我的 UI 按钮在经过一定时间后停止响应?

标签 ios swift

我的代码应该打印出当前记录的点击次数,它确实开始了,但随后随机停止了。有关解决此问题的任何提示?

我认为它与动画有关,但我不确定是什么原因导致应用停止响应。

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/

相关文章:

iOS Safari开发 ".local"域问题

ios - 如何在解析中从对象中获取 ObjectId 并在 swift 中将其显示为字符串

ios - 在 Swift 中更改多种货币的分组分隔符、货币符号和货币符号的位置

javascript - 在 tableView Swift 中使用 javascript 数据

ios - Xcode 6.0.1 Instruments iOS8 - 在设备上进行分析时应用程序表现不正常

iphone - 如何在 ios 中创建自己的面具

ios - 删除单元格导致崩溃

swift - 如何从 iOS 中完全删除 Realm 数据库?

ios - 如何在 swift 中显示 CGPDFDocument?

swift - 删除 Split View Controller 导航栏中的 1px 行