gpt4 book ai didi

swift - 连接 IBOutlet 时 IBAction 停止工作

转载 作者:行者123 更新时间:2023-11-30 11:36:53 24 4
gpt4 key购买 nike

我有一个 IBAction 连接到 Storyboard中的一个播放声音的按钮。这很好用。但是,当我将 IBOutlet 连接到同一个按钮时, socket 的代码将接管并且 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 不起作用。可能是什么原因造成的?它在 Storyboard中仍然是连接的。

最佳答案

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()
}

}

关于swift - 连接 IBOutlet 时 IBAction 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49650617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com