Search code examples
iosobjective-cuiviewcalayercabasicanimation

Changing a view's values and its layer's values


I'm animating an ImageView using CABasicAnimation. I move its layer to left, right, up and down and sometimes I'd scale it bigger then reset it to its original size etc.

I'm doing all this to its layer so I thought I might have to move & scale the real thing along with its layer as well but when I tested it with tap gesture to see if it really was just staying where it started, it wasn't. Therefore I no longer need to change its view's frames as far as I'm concerned.

Is changing a view's layer's values also change its view's values?


Solution

  • A UIView is no more than a fancy wrapper for a CALayer – bringing UIResponder events & animation conveniences among many other things.

    Many UIView properties are actually just forwarded versions of the underlying CALayer properties, defined purely for convenience.

    • A view's frame & bounds properties should always reflect the layer equivalents.

    • transform is slightly more complex, as for the view it's of type CGAffineTransform – whereas on the layer it's CATransform3D. If the layer's transform can be represented as a CGAffineTransform, then you'll be able to access it from the view after setting it on the layer. If it can't be represented, then its value is undefined.

    Therefore yes, you are right in saying you don't need to update the frame or transform on the UIView when changing it on its CALayer. Although note that these properties won't reflect the 'in-flight' values of the animation – you'll need to access the layer's presentationLayer for that.

    Also note that as @par & @jrturton mention, if a layer's transform is not the identity transform, then the frame is undefined and you therefore shouldn't use it.