1

In iOS, if UILabel inherits from UIView then why isn't the animate method available to UILabel and how would I have known to go up the class? I know its probably private to the UIView class but wouldn't it make sense to have it directly available to UILabel as well? How does Apple or developers dictate what methods should or should not be available?

Here's an example:

func animateLabelTransitions(){
        UIView.animate(withDuration: 3.5, animations: {self.questionLabel.alpha = 1})
    }
Laurence Wingo
  • 3,912
  • 7
  • 33
  • 61

2 Answers2

3

The animate methods are available to UILabel. They are class methods of UIView which means they are class methods of any subclass of UIView as well.

While pointless, you could have written your code as:

func animateLabelTransitions(){
    UILabel.animate(withDuration: 3.5, animations {
        self.questionLabel.alpha = 1
    })
}

But what's the point? This would confuse a lot of people. Just call the animate methods on UIView.

Just because you call animate on UIView doesn't mean it restricts what you can put inside the animation or completion blocks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • seems legit. If I were an Apple developer, I would just have a fear that since its a class method then it would affect all UIViews. Is this code visible to us the community to see how Apple put this method together? If this is a dumb question I apologize. – Laurence Wingo Nov 30 '17 at 15:20
  • 1
    It does animate all UIViews. And no, we do not have source code for it (though it's not that hard to reverse engineer most of these w/ Hopper… haven't tried this one, but always educational) – Rob Napier Nov 30 '17 at 15:25
  • @RobNapier I see, I'm guessing we directly call on which view we want to animate like I did above. Its making more sense, can I ask is there a method where we can grab all visible UIViews in a ViewController instead of just coding up each one directly? – Laurence Wingo Nov 30 '17 at 15:29
  • 2
    @Q.A.Neuro.Tech A class method means you can call it without needing a specific instance of the class. Don't confuse calling a class method with affecting all currently existing instances of that class. They have nothing to do with each other. There is nothing to fear. Calling a class method on `UIView` has no affect on any views unless that particular method happens to be written to affect clearly specified views such as the `animate` methods. Those affect only those views that you tell it to in the animation block. – rmaddy Nov 30 '17 at 15:34
  • OMG, I had to research on Google how to vote for the best answer just now. You are awesome and you've improved my questions over time with your earlier suggestions on edits in which I'm highly grateful for. You also gave me words of comfort and encouragement but the GUI placed @RobNapier answer at the top I'm guessing because he was first. My toughest decision of the day. – Laurence Wingo Nov 30 '17 at 15:53
2

animate is a class method, not an instance method; it applies to all views simultaneously, not some specific view. There's no need to specify which one thing you're trying to animate. You can put many things in the block and animate them all together. What would you expect to happen if you called UILabel.animate(...) and animated a button inside of the block? That would be totally legal (since it would just call the UIView version which can animate anything), but very confusing.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • nice answer! +1 – Reinier Melian Nov 30 '17 at 15:13
  • @RobNapier Thanks! Can I ask how do you know its a static method because when I look at the reference docs online, the animate function is prefixed with the word 'class'. For example on this Apple reference doc for an animate function: https://developer.apple.com/documentation/uikit/uiview/1622515-animate – Laurence Wingo Nov 30 '17 at 15:16
  • 1
    Yes; class method. There isn't a strong distinction in ObjC. – Rob Napier Nov 30 '17 at 15:25
  • @RobNapier I'm going to vote you as the best answer since the GUI placed you at the top for being the first I'm guessing. If you can, could you elaborate on your opinion as to why there isn't a strong distinction between static and class methods in ObjC? – Laurence Wingo Nov 30 '17 at 15:56
  • 1
    ObjC doesn't have static methods at all; it only has class methods. Read https://stackoverflow.com/questions/8089186/whats-the-difference-between-class-method-and-static-method/8089623#8089623 for a good overview. But also notice the wildly divergent terms and meanings ObjC devs use in the comments and answers. bbum is certainly correct (he usually is on most topics), but the muddling of the terms is very common in the Cocoa world. I didn't mean to confuse you here. – Rob Napier Nov 30 '17 at 16:18