【问题标题】:IBAction stops working when I connect IBOutlet当我连接 IBOutlet 时,IBAction 停止工作
【发布时间】:2018-04-04 12:07:40
【问题描述】:

我有一个 IBAction 连接到我的故事板中播放声音的按钮。这工作得很好。但是,当我将 IBOutlet 连接到同一个按钮时,插座的代码会接管并且 IBAction 停止工作。在这种情况下,IBOutlet 的作用是使按钮带有动画脉冲。这是我的代码:

import UIKit
import Firebase
import AVFoundation

class Page1: UIViewController, AVAudioPlayerDelegate, UIAlertViewDelegate {
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var pageControl: UIPageControl!
    @IBOutlet weak var testpulse: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = Bundle.main.url(forResource: "ahh", withExtension: "mp3")

        do {
            ahh = try AVAudioPlayer(contentsOf: url!)
            ahh.prepareToPlay()

        }
        catch let error as NSError {
            print(error.debugDescription)
        }

        testpulse.isUserInteractionEnabled = true

        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(Page1.addPulse))
        tapGestureRecognizer.numberOfTapsRequired = 1
        testpulse.addGestureRecognizer(tapGestureRecognizer)


    }

    @objc func addPulse(){
        let pulse = Pulsing(numberOfPulses: 1, radius: 110, position: testpulse.center)
        pulse.animationDuration = 0.8
        pulse.backgroundColor = UIColor.purple.cgColor

        self.view.layer.insertSublayer(pulse, below: testpulse.layer)
    }

    @IBAction func ahh(_ sender: Any) {
        ahh.play()
    }  
}

对于我的 Swift 动画文件,我有这个:

import UIKit

class Pulsing: CALayer {

    var animationGroup = CAAnimationGroup()

    var initialPulseScale:Float = 0
    var nextPulseAfter:TimeInterval = 0
    var animationDuration:TimeInterval = 1.5
    var radius:CGFloat = 200
    var numberOfPulses:Float = Float.infinity

    override init(layer: Any) {
        super.init(layer: layer)
    }

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

    init (numberOfPulses:Float = Float.infinity, radius:CGFloat, position:CGPoint) {
        super.init()

        self.backgroundColor = UIColor.black.cgColor
        self.contentsScale = UIScreen.main.scale
        self.opacity = 0
        self.radius = radius
        self.numberOfPulses = numberOfPulses
        self.position = position

        self.bounds = CGRect(x: 0, y: 0, width: radius * 2, height: radius * 2)
        //self.cornerRadius = radius

        DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
            self.setupAnimationGroup()

            DispatchQueue.main.async {
                self.add(self.animationGroup, forKey: "pulse")
            }
        }

        setupAnimationGroup()

        self.add(animationGroup, forKey: "pulse")
    }

    func createScaleAnimation () -> CABasicAnimation {
        let scaleAnimation = CABasicAnimation(keyPath: "transform.scale.xy")
        scaleAnimation.fromValue = NSNumber(value: initialPulseScale)
        scaleAnimation.toValue = NSNumber(value: 1)
        scaleAnimation.duration = animationDuration

        return scaleAnimation
    }

    func createOpacityAnimation() -> CAKeyframeAnimation? {

        let opacityAnimation = CAKeyframeAnimation(keyPath: "opacity")
        opacityAnimation.duration = animationDuration
        opacityAnimation.values = [0.4, 0.8, 0]
        opacityAnimation.keyTimes = [0, 0.2, 1]

        return opacityAnimation
    }

    func setupAnimationGroup() {
        self.animationGroup = CAAnimationGroup()
        self.animationGroup.duration = animationDuration + nextPulseAfter
        self.animationGroup.repeatCount = numberOfPulses

        let defaultCurve = CAMediaTimingFunction(name: kCAMediaTimingFunctionDefault)
        self.animationGroup.timingFunction = defaultCurve

        self.animationGroup.animations = [createScaleAnimation(), createOpacityAnimation()!]
    }
}

当我将 IBAction 连接到我的按钮时,声音会起作用。但是,当我连接 IBOutlet 时,动画有效,但 IBAction 无效。这可能是什么原因造成的?它仍然在情节提要中连接。

【问题讨论】:

  • 为什么要使用单独的手势识别器进行脉冲?为什么不在你的“ahh”方法中调用“addPulse”?
  • 在执行操作之前使用if sender.isKind(of: UIButton) 获取类,或者在操作方法中将类设置为UIButton 而不是Any
  • 您的问题可能不是来自添加 IBOutlet,而是来自手势识别器。您是在告诉按钮在 IBAction 连接中以一种方式响应点击,而在代码中以不同方式响应。
  • 我预感它是手势识别器。我正在尝试从动画代码创建一个 IBAction,但我不太确定如何做到这一点,因为我最初遵循了一个教程。我确信我需要像 Olter 建议的那样在我的方法中调用一些东西。

标签: swift xcode


【解决方案1】:
import UIKit
import Firebase
import AVFoundation

class Page1: UIViewController, AVAudioPlayerDelegate, UIAlertViewDelegate {
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var pageControl: UIPageControl!
    @IBOutlet weak var testpulse: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = Bundle.main.url(forResource: "ahh", withExtension: "mp3")

        do {
            ahh = try AVAudioPlayer(contentsOf: url!)
            ahh.prepareToPlay()

        }
        catch let error as NSError {
            print(error.debugDescription)
        }

        testpulse.isUserInteractionEnabled = true
    }

    @objc func addPulse(){
        let pulse = Pulsing(numberOfPulses: 1, radius: 110, position: testpulse.center)
        pulse.animationDuration = 0.8
        pulse.backgroundColor = UIColor.purple.cgColor

        self.view.layer.insertSublayer(pulse, below: testpulse.layer)
    }

    @IBAction func ahh(_ sender: Any) {
        ahh.play()
        addPulse()
    }  

}

【讨论】:

  • 感谢您的回答。我看到这里的目标是识别多个手势识别器。我尝试了这段代码,不幸的是它的工作原理几乎相同。我以前没有使用过扩展。你把它放在课前吗?那是我唯一可以放置它而不会出现任何错误的地方。
  • 我已经编辑了我的答案,只是复制粘贴而不是你的班级
  • 嘿,这成功了!我就知道应该这么简单。尽管如此,我还是花了一点时间让它工作。非常感谢。
猜你喜欢
  • 2020-11-22
  • 2017-05-10
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多