点击手势在Circle Swift上无效

00jrzges  于 2023-09-29  发布在  Swift
关注(0)|答案(2)|浏览(98)

我正在创建一个单页游戏,你选择一个圆圈,得分水平增加。我几乎完成了,但我的点击手势对圆圈不起作用。我试过下面的代码,但它不工作。请检查下面的代码。

import UIKit
import AVFoundation

class Demogame: UIViewController {

    @IBOutlet weak var scoreLabel: UILabel!

    var score = 0
    var isGameOver = false

    override func viewDidLoad() {
        super.viewDidLoad()
        startGame()
    }

    func startGame() {
        score = 0
        scoreLabel.text = "Score: \(score)"
        isGameOver = false

        // Create a timer to spawn circles
        Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { timer in
            if !self.isGameOver {
                self.spawnCircle()
            } else {
                timer.invalidate()
            }
        }
    }

    func spawnCircle() {
        let circleSize = CGSize(width: 50, height: 50)
        let circleView = UIView(frame: CGRect(origin: CGPoint(x: CGFloat.random(in: 50...view.frame.width - circleSize.width - 50),
                                                              y: CGFloat.random(in: 100...view.frame.height - circleSize.height - 50)),
                                              size: circleSize))

        circleView.backgroundColor = UIColor.random()
        circleView.layer.cornerRadius = circleSize.width / 2
        view.addSubview(circleView)

        // Add a tap gesture recognizer to each circle
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
        circleView.addGestureRecognizer(tapGesture)

        // Animate the circle's appearance and disappearance
        UIView.animate(withDuration: 3.0, animations: {
            circleView.alpha = 0.0
        }) { (completed) in
            if completed {
                circleView.removeFromSuperview()
            }
        }
    }

    @objc func handleTap(_ sender: UITapGestureRecognizer) {
        if isGameOver {
            return
        }

        if let tappedView = sender.view {
            tappedView.removeFromSuperview()
            score += 1
            scoreLabel.text = "Score: \(score)"
        }
    }
}

extension UIColor {
    static func random() -> UIColor {
        return UIColor(
            red: CGFloat.random(in: 0.0...1.0),
            green: CGFloat.random(in: 0.0...1.0),
            blue: CGFloat.random(in: 0.0...1.0),
            alpha: 1.0
        )
    }
}

问:如何点击手势工作的圆圈和增加得分水平?
有人可以请向我解释如何做到这一点,我已经尝试了上述代码,但还没有结果。如果我做错了请纠正我。
任何帮助将不胜感激

fkaflof6

fkaflof61#

视图-按钮、标签、图像视图、平面视图等-当.alpha小于0.01时,不接收触摸
可能不明显的是,当我们编写这样的动画块时:

UIView.animate(withDuration: 3.0, animations: {
    circleView.alpha = 0.0
})

UIKit立即评估块内的指令,然后计算动画视觉效果。
因此,一旦动画 * 开始 * UIKit就认为circleView具有0.0.alpha-因此,没有触摸事件/点击手势。
另一个问题是,默认情况下,您不能在动画期间与UI元素交互。
您可以通过将动画选项设置为.allowUserInteraction来解决此问题。
如果将动画块更改为:

UIView.animate(withDuration: 3.0, delay: 0.0, options: .allowUserInteraction, animations: {
    circleView.alpha = 0.05
}, completion: { b in
    if b {
        circleView.removeFromSuperview()
    }
})

您现在应该能够点击圆圈。

qvk1mo1f

qvk1mo1f2#

将动画块重写为以下内容:

UIView.animate(withDuration: 3.0,
               delay: 0.0,
               options: .allowUserInteraction
               animations: { circleView.backgroundColor = .clear },
               completion: { completed in
        if completed {
            circleView.removeFromSuperview()
        }
})

1.将视图alpha设置为零将禁用点击测试,因此请改用透明颜色。
1.添加“允许用户交互”选项到您的动画。

相关问题