I am making an app where I have a button
who is moving from one side of the screen to the other. I have created a pause button
which pauses the animation once that button
is selected. This button
and function works fine. I also added a function which determines if the user has exited the application. In this function, I added the pause button
function. However, when the user returns to the app after exiting, it shows the pause screen, but the animation is completed. Here is the code:
@IBAction func pauseButton(sender: UIButton) {
let button = self.view.viewWithTag(1) as? UIButton!
let layer = button!.layer
pauseLayer(layer) // Pausing the animation
view2.hidden = false // Showing pause screen
}
func pauseLayer(layer: CALayer) {
let pausedTime: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
layer.speed = 0.0
layer.timeOffset = pausedTime
}
@IBAction func startButton(sender: UIButton) {
let button = UIButton()
button.tag = 1 // so I can retrieve it in the pause function
// Extra code to make the button look nice
button.translatesAutoresizingMaskIntoConstraints = false // Allow constraints
let topAnchorForButton = button.topAnchor.constraintEqualToAnchor(view.topAnchor, constant: 400)
NSLayoutConstraint.activateConstraints([ // Adding constraints
topAnchorForButton,
button.leadingAnchor.constraintEqualToAnchor(view.topAnchor, constant: 120),
button.widthAnchor.constraintEqualToConstant(65),
button.heightAnchor.constraintEqualToConstant(65)
])
self.view.layoutIfNeeded()
[UIView.animateWithDuration(5.0, delay: 0.0, options: [.CurveLinear, .AllowUserInteraction], animations: {
topAnchorForButton.constant = 0
self.view.layoutIfNeeded()
}, completion: nil )] // the animation
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseButton:", name: UIApplicationWillResignActiveNotification, object: nil) // When exists app
NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseButton:", name: UIApplicationDidEnterBackgroundNotification, object: nil) // when goes to home screen
}
When I re-enter the app, the animation is completed.
Here's is a code I've tried which only plays and stops the animation. I've also added something in the App Delegate but it still doesn't work:
ViewController:
class Home: UIViewController {
func pauseAnimation(){
let button = self.view.viewWithTag(1) as? UIButton!
let layer = button!.layer
pauseLayer(layer) // Pausing the animation
}
@IBAction func pauseButton(sender: UIButton) {
pauseAnimation()
}
func pauseLayer(layer: CALayer) {
let pausedTime: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
layer.speed = 0.0
layer.timeOffset = pausedTime
}
@IBAction func startButton(sender: UIButton) {
let button = UIButton()
button.tag = 1 // so I can retrieve it in the pause function
// Extra code to make the button look nice
button.setTitle("Moving", forState: .Normal)
button.setTitleColor(UIColor.redColor(), forState: .Normal)
view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false // Allow constraints
let topAnchorForButton = button.topAnchor.constraintEqualToAnchor(view.topAnchor, constant: 400)
NSLayoutConstraint.activateConstraints([ // Adding constraints
topAnchorForButton,
button.leadingAnchor.constraintEqualToAnchor(view.topAnchor, constant: 120),
button.widthAnchor.constraintEqualToConstant(65),
button.heightAnchor.constraintEqualToConstant(65)
])
self.view.layoutIfNeeded()
[UIView.animateWithDuration(10.0, delay: 0.0, options: [.CurveLinear, .AllowUserInteraction], animations: {
topAnchorForButton.constant = 0
self.view.layoutIfNeeded()
}, completion: nil )] // the animation
}
App Delegate:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var vc = Home()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
}
func applicationWillResignActive(application: UIApplication) {
vc.pauseAnimation()
}
Just in case you want to know, here is the code for resuming the animation:
@IBAction func resume(sender: UIButton) {
let button = self.view.viewWithTag(100) as? UIButton!
let layer = button!.layer
resumeLayer(layer)
}
func resumeLayer(layer: CALayer) {
let pausedTime: CFTimeInterval = layer.timeOffset
layer.speed = 1.0
layer.timeOffset = 0.0
layer.beginTime = 0.0
let timeSincePause: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime
layer.beginTime = timeSincePause
}
When I exit the app, it says that button
or/and layer
is nil (fatal error... optional value is nil), so it crashes.
Please help. Thanks in advance... Anton