【问题标题】:How to stop my UIButton animation SWIFT如何停止我的 UIButton 动画 SWIFT
【发布时间】:2016-02-17 19:03:32
【问题描述】:

我通过一些图像使用 UIButton 制作了动画。 代码如下:

         @IBAction func Touchdown(sender: AnyObject) {
    Izer.setImage(image1, forState: UIControlState.Normal)
    Izer.imageView!.animationImages = [image1, image2, image3, image4,                    image5,
        image6, image7,image8]
    Izer.imageView!.animationDuration = 0.9
    Izer.imageView!.startAnimating()
            playButton.enabled = false



}





@IBAction func TouchUp(sender: AnyObject) {

    soundRecorder.stop()
    playButton.enabled = true


}

当我触摸按钮时,动画开始。但我想通过我的 Touchup 功能停止它。

我该怎么做?

谢谢你,对不起我的英语不好:(

【问题讨论】:

  • 您是否尝试将Izer.imageView!.stopAnimating() 放入您的TouchUp 方法中?
  • @Phoen1xUK Owwww 人谢谢你!!!
  • 只是将其添加为答案,以便我们可以关闭问题:)

标签: swift animation button


【解决方案1】:

将此添加到您的TouchUp func:

Izer.imageView!.stopAnimating()

附言查找函数信息的好地方是 Apple 文档——它确实非常好。所以this is the page for an imageView 如果您在tasks 下查看左侧,或者向下滚动,您可以看到您可以调用和设置的函数和属性来为imageView 设置动画。

【讨论】:

    【解决方案2】:

    好的,看起来您希望该按钮充当切换开关。在第一次触摸按钮开始动画图像视图并记录一些东西。再次触摸时,动画停止,按钮再次启用。

    您可以通过声明一个跟踪按钮状态的布尔变量来实现这一点。如果布尔值设置为 true,则运行动画和录制。如果布尔值为 false,则停止并记录。

    这里是示例代码:

    class ViewController: UIViewController {
    
    @IBOutlet weak var mainView: UIView!
    var isButtonPressed = false{
        // Adding a Property Observer, that reacts to changes in button state
        didSet{
            if isButtonPressed{
                // Run the Recording function & Animation Function.
                startAnimationAndRecording()
            }else{
                // Stop the Recoding function & Animation Function.
                stopAnimationAndRecording()
            }
        }
    }
    
    @IBAction func changeButtonValue(sender: UIButton) {
        // Toggle the button value.
        isButtonPressed = !isButtonPressed
    }
    
    func startAnimationAndRecording(){
        // Add your animation and recording code here.
    }
    
    func stopAnimationAndRecording(){
        //Add your stop Animation & Stop Recording code here.
    }
    

    }

    【讨论】:

    • 嘿,谢谢你的信息!但我不知道该放什么代码来停止动画...
    猜你喜欢
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    相关资源
    最近更新 更多