Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Core Animation does make this sort of thing trivial. Brad's suggestion is right on. The bottom line is that you don't need any of those methods to simply <em>render</em> the layer. In order to cause the layer to render, make sure you've done the following:</p> <ul> <li><p>Set the contents property with:</p> <p><code>[imageLayer setContents:(id)[[UIImage imageNamed@"image.png"] CGImage]];</code></p></li> <li><p>Set the bounds of the layer to the size you want.</p> <p><code>[imageLayer setBounds:CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)];</code></p></li> <li><p>Set the position (x,y location) of the layer to display in the view. The default anchorPoint is the center of the layer. This code centers the layer in your view.</p> <p><code>[imageLayer setPosition:CGPointMake([view bounds].size.width/2, [view bounds].size.height/2)];</code></p></li> <li><p>Add the layer to your view's layer:</p> <p><code>[[[self view] layer] addSublayer:imageLayer];</code></p></li> </ul> <p>Incidentally, if you prefer, you can set both bounds and position in one method by calling -setFrame:. I prefer to use the two calls myself as it feels more readable to me, but that's up to your own preference. <em>If you do not set the bounds and position or frame of the layer, however, the layer will not render.</em></p> <p>If you would prefer, you can avoid using drawInContext by creating additional layers that draw paths, shapes (see CAShaperLayer), or additional images and add them as sublayers of your image layer or add them as sublayers of your parent layer and give them a zPosition that causes them to display in front of your image layer.</p> <p>Now, if you want to animate the opacity, you can use implicit animation by simply setting the layer property in the exact manner you described, e.g. [imageLayer setOpacity:0.0f]; This will fade the layer and all of it's child layers over 0.25 seconds.</p> <p>Just some additional thoughts.</p> <p>Best Regards.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload