Graphics Reference
In-Depth Information
new sprites or need to change the dimensions of any of the existing ones after the sheet has
been created.
Several commercial applications are available for creating sprite sheets automatically on
your Mac. These tools simplify the use of sprites by automatically generating an XML or
Property List (Plist) file containing the sprite coordinates. This file can then be loaded along
with the image and used to set the contentsRect for each sprite, instead of the
developer having to manually code the positions into the application.
These files are usually designed to be used in OpenGL games, but if you are interested in
using sprite sheets in a regular app, the open source LayerSprites library
( https://github.com/nicklockwood/LayerSprites ) can read sprite sheets in the popular
Cocos2D format and display them using ordinary Core Animation layers.
contentsCenter
The last contents-related property we look at in this chapter is contentsCenter . You
might expect from the name that contentsCenter would have something to do with the
position of the contents image, but the name is misleading. The contentsCenter is
actually a CGRect that defines a stretchable region inside the layer and a fixed border
around the edge. Changing the contentsCenter makes no difference to how the
backing image is displayed, until the layer is resized , and then its purpose becomes clear.
By default, the contentsCenter is set to {0, 0, 1, 1}, which means that the backing
image will stretch uniformly when the layer is resized (depending on the
contentsGravity ). But if we increase the origin values and reduce the size, we can
create a border around the image. Figure 2.9 shows how the scaling works for a
contentsCenter value of {0.25, 0.25, 0.5, 0.5}.
Figure 2.9 An example of a contentsCenter rectangle, and the effect it has on the image
Search WWH ::




Custom Search