Understanding UIView Animation In Swift

Viewed 37

I know how to animate UIViews using the animate() function. What I'm trying to understand is why the animation takes place on the class in general, as opposed to a specific instance. I don't know the right words to describe this so I couldn't find any resources on this.

So for example, if I wanted to use SpriteKit and I wanted to move a node, I'd use node.run(SKAction.move(to: CGPoint.zero, 0.5). This makes a lot of sense to me. It defines an SKAction (so it could be used with other nodes if needed), and then applies it to a specific node.

If I wanted to do the same with a view, I'd do something like:

exampleViewConstraint.constant = 0
UIView.animate(withDuration: 0.5) { [weak self] in
  self?.view.layoutIfNeeded()
}

Aside from this being much more complicated verbiage, I don't understand why this is being called on the class in general. I'm having a hard time conceptualizing. What exactly is happening here?

  • When you call a function on a general class, what does that do? Are there other examples where that's used? Why do you need to use a class function here?
  • I get that the closure captures the parent class's view, but what if there was another view elsewhere in the same function? How could I specify that it animates view 1 but not view 2?
  • Why must this be done in such a confusing way?

Thanks in advance for your help!

0 Answers
Related