How to start an animation without button Tap

Hello:


I've read just about every thing I could find about animations in my MAC OSx app. However, I've not been able to find a useful tutorial of example. Nearly All the examples i found are for iOS and all of those animations are initiated by tapping a button.


I want to be able to make the image on my main window Zoom in from a small icon to fill the window in a gradual zoom in motion as the app main View is loaded.


I will appreciate any example, tutorial, or Swift resource that could be useful in solving this problem.


Thanks in Advance.

Answered by OOPer in 423893022

I am using Xcode version 11.5.

The same as mine. So, the version has nothing to do with your crash.


with the width and height of the ImageView set to 48 and priority 750,

I wrote that you should remove all the constraints except center. Please follow my suggestions when you try my code.


the app still crashes with the error "Unexpectedly found nil while implicitly unwrapping an Optional value: file" on line18

If it happens on line 18. `widthConstraint = imageView.widthAnchor.constraint(equalToConstant: 48)`, then `imageView` is nil.

Maybe the outlet connection to `imageView` is broken in your storyboard. (Sometimes Xcode shows false `connected`-indicator.)

Disconnect and re-connect the IBOutlet to the right NSImageView.


In case I was missing some hidden setup in the storyboard, I made another version with programmatic setup for `imageView`.

Please try this code with NSImageView removed in the storyboard. So that all the constraints are removed and no need to worry about IBOutlet connection.

(Assuming you have the image resource `icon.jpg` and `NSImage(named: "icon.jpg")!` would not cause Unexpectedly found nil.)


import Cocoa

class ViewController: NSViewController {

    var imageView: NSImageView!
    
    var widthConstraint: NSLayoutConstraint!
    var heightConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        imageView = NSImageView(image: NSImage(named: "icon.jpg")!)
        self.view.addSubview(imageView)
        
        imageView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        imageView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
        imageView.translatesAutoresizingMaskIntoConstraints = false

        widthConstraint = imageView.widthAnchor.constraint(equalToConstant: 48)
        widthConstraint.isActive = true
        heightConstraint = imageView.heightAnchor.constraint(equalToConstant: 48)
        heightConstraint.isActive = true
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        
        NSAnimationContext.runAnimationGroup({context in
            context.duration = 1.0
            widthConstraint.animator().constant = self.view.frame.width
            heightConstraint.animator().constant = self.view.frame.height
        }) {
            print("Animation finished")
            //Do some UI updates on animation finished, if you need...
        }
    }

}
Accepted Answer

I am using Xcode version 11.5.

The same as mine. So, the version has nothing to do with your crash.


with the width and height of the ImageView set to 48 and priority 750,

I wrote that you should remove all the constraints except center. Please follow my suggestions when you try my code.


the app still crashes with the error "Unexpectedly found nil while implicitly unwrapping an Optional value: file" on line18

If it happens on line 18. `widthConstraint = imageView.widthAnchor.constraint(equalToConstant: 48)`, then `imageView` is nil.

Maybe the outlet connection to `imageView` is broken in your storyboard. (Sometimes Xcode shows false `connected`-indicator.)

Disconnect and re-connect the IBOutlet to the right NSImageView.


In case I was missing some hidden setup in the storyboard, I made another version with programmatic setup for `imageView`.

Please try this code with NSImageView removed in the storyboard. So that all the constraints are removed and no need to worry about IBOutlet connection.

(Assuming you have the image resource `icon.jpg` and `NSImage(named: "icon.jpg")!` would not cause Unexpectedly found nil.)


import Cocoa

class ViewController: NSViewController {

    var imageView: NSImageView!
    
    var widthConstraint: NSLayoutConstraint!
    var heightConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        imageView = NSImageView(image: NSImage(named: "icon.jpg")!)
        self.view.addSubview(imageView)
        
        imageView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        imageView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
        imageView.translatesAutoresizingMaskIntoConstraints = false

        widthConstraint = imageView.widthAnchor.constraint(equalToConstant: 48)
        widthConstraint.isActive = true
        heightConstraint = imageView.heightAnchor.constraint(equalToConstant: 48)
        heightConstraint.isActive = true
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        
        NSAnimationContext.runAnimationGroup({context in
            context.duration = 1.0
            widthConstraint.animator().constant = self.view.frame.width
            heightConstraint.animator().constant = self.view.frame.height
        }) {
            print("Animation finished")
            //Do some UI updates on animation finished, if you need...
        }
    }

}

Hello 00per:


Your code works beautifully!!

Thanks a million. You're the greatest.

How to start an animation without button Tap
 
 
Q