应用程序退出后,animateWithDuration()停止

时间:2015-12-15 00:35:44

标签: ios swift uibutton animatewithduration

我已将发光效果合并到我的UIButtons中(代码取自https://github.com/bc-oscar/Glowing-UIButton)。代码位于单独的swift文件中,按钮在视图控制器中定义为“GlowingButton”类型,并更改为从身份检查器中的此自定义类派生。当应用程序第一次运行时效果有效,但在退出后返回到应用程序时会停止吗?

import UIKit 

class GlowingButton: UIButton {

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
    // Drawing code
}
*/

// Creates a glow effect in the button by setting its layer shadow properties
func startGlowWithCGColor (growColor:CGColor) {
    self.layer.shadowColor = growColor
    self.layer.shadowRadius = 10.0
    self.layer.shadowOpacity = 1.0
    self.layer.shadowOffset = CGSizeZero
    self.layer.masksToBounds = false

    // Autoreverse, Repeat and allow user interaction.
    UIView.animateWithDuration(1.0, delay: 0, options: [UIViewAnimationOptions.Autoreverse,                                                         UIViewAnimationOptions.CurveEaseInOut, UIViewAnimationOptions.Repeat, UIViewAnimationOptions.AllowUserInteraction],
        animations: { () -> Void in
            // Make it a 15% bigger
            self.transform = CGAffineTransformMakeScale(1.15, 1.15)
        }) { (Bool) -> Void in
            // Return to original size
            self.layer.shadowRadius = 0.0
            self.transform = CGAffineTransformMakeScale(1.0, 1.0)
    }
}

// Removes the animation
func stopGlow () {
    self.layer.shadowRadius = 0.0
    self.transform = CGAffineTransformMakeScale(1.0, 1.0)
    self.layer.removeAllAnimations()
    self.layer.masksToBounds = true
}

}

2 个答案:

答案 0 :(得分:0)

那是因为您在startGlowWithCGColor中呼叫viewDidLoad,请尝试将其放入viewDidAppear

https://github.com/bc-oscar/Glowing-UIButton/blob/master/SampleApp/Sample%20App/ViewController.swift#L24

答案 1 :(得分:0)

我不完全明白导致这种情况的原因,但似乎退出应用程序会使功能停留在self.transform = CGAffineTransformMakeScale(1.0, 1.0)上。递归调用self.startGlowWithCGColor(growColor)函数会重新启动效果。