Swift - 添加UIView后无法点击按钮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift - 添加UIView后无法点击按钮相关的知识,希望对你有一定的参考价值。

所以,我有一个类创建了UIView的Confetti子类,它在达到高分时运行。但是,当五彩纸屑运行时,我的按钮变得不可点击,这使它成为问题。当没有运行五彩纸屑时,我的按钮是可点击的,没有错误。请帮助我,我一直试图解决这个问题几个小时。

这是我运行Confetti的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    if UserDefaults.standard.integer(forKey: "Score") > UserDefaults.standard.integer(forKey: "Highscore") {
confettiView.startConfetti()
        delay(3, completion: {
            confettiView.stopConfetti()
        })
}

这是我创建五彩纸屑的代码:

import UIKit
import QuartzCore

public class SAConfettiView: UIView {

    public enum ConfettiType {
        case Confetti
        case Triangle
        case Star
        case Diamond
        case Image(UIImage)
    }

    var emitter: CAEmitterLayer!
    public var colors: [UIColor]!
    public var intensity: Float!
    public var type: ConfettiType!
    private var active :Bool!

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    public override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    func setup() {
        colors = [UIColor(red:0.95, green:0.40, blue:0.27, alpha:1.0),
        UIColor(red:1.00, green:0.78, blue:0.36, alpha:1.0),
        UIColor(red:0.48, green:0.78, blue:0.64, alpha:1.0),
        UIColor(red:0.30, green:0.76, blue:0.85, alpha:1.0),
        UIColor(red:0.58, green:0.39, blue:0.55, alpha:1.0)]
        intensity = 0.5
        type = .Confetti
        active = false
    }

    public func startConfetti() {
        emitter = CAEmitterLayer()
        emitter.emitterPosition = CGPoint(x: frame.size.width / 2.0, y: 0)
        emitter.emitterShape = kCAEmitterLayerLine
        emitter.emitterSize = CGSize(width: frame.size.width, height: 1)

        var cells = [CAEmitterCell]()
        for color in colors {
            cells.append(confettiWithColor(color: color))
        }

        emitter.emitterCells = cells
        layer.addSublayer(emitter)
        active = true
    }

    public func stopConfetti() {
        emitter?.birthRate = 0
        active = false
    }

    func imageForType(type: ConfettiType) -> UIImage? {

        var fileName: String!

        switch type {
            case .Confetti:
            fileName = "confetti"
            case .Triangle:
            fileName = "triangle"
            case .Star:
            fileName = "star"
            case .Diamond:
            fileName = "diamond"
            case let .Image(customImage):
            return customImage
        }
       /*
        let path = Bundle(for: SAConfettiView.self).path(forResource: "SAConfettiView", ofType: "bundle")
        let bundle = Bundle(path: path!)
        let imagePath = bundle?.path(forResource: fileName, ofType: "png")
        let url = NSURL(fileURLWithPath: imagePath!)
        let data = NSData(contentsOf: url as URL)
        if let data = data {
            return UIImage(data: data as Data)!
        }
        return nil */
        return UIImage(named: fileName)
    }

    func confettiWithColor(color: UIColor) -> CAEmitterCell {
        let confetti = CAEmitterCell()
        confetti.birthRate = 6.0 * intensity
        confetti.lifetime = 14.0 * intensity
        confetti.lifetimeRange = 0
        confetti.color = color.cgColor
        confetti.velocity = CGFloat(350.0 * intensity)
        confetti.velocityRange = CGFloat(80.0 * intensity)
        confetti.emissionLongitude = CGFloat(M_PI)
        confetti.emissionRange = CGFloat(M_PI_4)
        confetti.spin = CGFloat(3.5 * intensity)
        confetti.spinRange = CGFloat(4.0 * intensity)
        confetti.scaleRange = CGFloat(intensity)
        confetti.scaleSpeed = CGFloat(-0.1 * intensity)
        confetti.contents = imageForType(type: type)!.cgImage
        return confetti
    }

    public func isActive() -> Bool {
        return self.active
    }
}
答案

开始运行五彩纸屑后,将按钮子视图置于正面。

希望这可以帮助。

另一答案

看来你的新视图模糊了它下面的按钮视图。理想情况下,在完成所有操作后,您应该从stopConfetti()方法中的superview中删除五彩纸屑视图。

以上是关于Swift - 添加UIView后无法点击按钮的主要内容,如果未能解决你的问题,请参考以下文章

iOS Swift 3 - 面板点击后删除覆盖 UIView

在 Swift 中将多个 UIButton 添加到多个 UIView

Swift Sprite Kit 应用内购买

自定义按钮无法点击 Swift

按钮是不可点击的 swift

swift怎么在点击按钮时重新调用uiview的drawrect函数