---
title: "UIKit Animations with TexturePacker"
description: "Use TexturePacker to create animations for UIKit to save memory and decrease loading time"
canonical: "https://www.codeandweb.com/texturepacker/tutorials/uikit-animations-with-texturepacker"
date: "2023-09-21T00:00:00.000Z"
modified: "2023-09-21T00:00:00.000Z"
source: "html"
---

# UIKit Animations with TexturePacker

Andreas Löw

[texturepacker](https://www.codeandweb.com/texturepacker/tutorials.md),  [tutorial](https://www.codeandweb.com/tags/tutorial),  [uikit](https://www.codeandweb.com/tutorials/uikit.md)

[GitHub](https://github.com/CodeAndWeb/uikit-sprite-sheet-example)

![UIKit Animations with TexturePacker](https://www.codeandweb.com/static/c632d7f4cb72b7108d54f93293a24ddd/a6312/post-image.png)

What you are going to learn:

*   Using the sprite sheets with UIKit
*   Add static images
*   Add and control animations

Full demo source code including CAWSpriteReader and CAWSpriteLayer classes is available on [GitHub](https://github.com/CodeAndWeb/uikit-sprite-sheet-example).

UIKit applications often include animations and extensive graphics. Adding graphics as individual images can lead to high memory consumption and impede performance.

Using sprite sheets, rather than individual images, can enhance performance by:

*   Reducing memory usage
*   Accelerating application loading

## Creating a sprite sheet

The easiest way to create optimized sprite sheets is using TexturePacker. You can download it from here — it's available for Windows, macOS and Linux:

[Download TexturePacker 9.0.0](https://www.codeandweb.com/texturepacker/download/9.0.0/win)

for Windows

[Also available for macOS and Linux](https://www.codeandweb.com/texturepacker/download.md)

You can find an introduction on how to create a sprite sheet with TexturePacker [here](https://www.codeandweb.com/texturepacker/tutorials/how-to-create-a-sprite-sheet.md). The demo project on [GitHub](https://github.com/CodeAndWeb/uikit-sprite-sheet-example) already includes a TexturePacker project named _SpriteSheet.tps_, which contains sample sprites and sprite sheet configuration.

  ![Using TexturePacker to create sprite sheets](https://www.codeandweb.com/static/89f0770d2dcd55b1433485652d03683c/283d3/spritesheet.png)

## Using the sprites with UIKit

I've created some powerful classes which allow you to simply load the sprite sheets created with TexturePacker into your application - including code to play animations.

If you use animations that are pre-aligned, you might also have a lot of transparent pixels in your images. While the individual files might not use much memory - due to compression - the RAM usage might be quite big.

If you pack your sprite with TexturePacker this transparency can be removed. The provided `CAWSpriteLayer` class compensates the missing transparency by moving the sprite into the same spot as if it had its original size.

First of all, you need to load the data file created with TexturePacker and the sprite sheet image:

```objc
NSDictionary *spriteData = [CAWSpriteReader spritesWithContentOfFile:@"spritesheet.plist"];
UIImage *texture = [UIImage imageNamed:@"spritesheet.png"];
```

With this you can simply create layers and add them to your current view:

```objc
CAWSpriteLayer *layer = [CAWSpriteLayer layerWithSpriteData:spriteData andImage:texture];
[self.view.layer addSublayer:layer];
```

### Static images

To add a static image use:

```objc
[layer showFrame:@"staticimage"];
```

With this you can use all standard operations on layers - like setting the position or applying transformations:

```objc
[layer setPosition:CGPointMake(400, 200)];
[layer setTransform:CATransform3DMakeScale(0.5,0.5, 1.0)];
```

### Animations

To add an animation use:

```objc
[layer playAnimation:@"CapGuyWalk%04d" withRate:24];
```

The format of the animation's name is derived from its frame names: This will play an animation with all frames that match `CapGuyWalk0000` , `CapGuyWalk0001` , `CapGuyWalk0002` , ... until no more frames with matching names are found.

The frame rate is set to 24 frames per second.

You can also set animations to repeat:

```objc
[layer playAnimation:@"CapGuyWalk%04d" withRate:24 andRepeat:2];
```

The special value `INFINITY` will repeat the animation forever.

After an animation is played to the end, you can decide if you want to show the last frame or an empty frame:

```objc
[layer setShowLastFrame:true];
```

### Controlling animations

The `CAWSpriteLayer` has all standard animation controls:

Pause:

```objc
[layer pause];
```

Resume:

```objc
[layer resume];
```

Stop:

```objc
[layer stop];
```

## Internal structure

The `CAWSpriteLayer` consists of 2 nested layers: The outer `CAWSpriteLayer` which allows you to perform all transformations, and the inner `CAWSpriteCoreLayer` which handles the sprite display. This is necessary since the inner layer makes use of transformations to implement trimming.

## Demo Project

I've created a complete demo project for you - available on [GitHub](https://github.com/CodeAndWeb/uikit-sprite-sheet-example) for download.

## Structured data

```json
{
  "@context": "https://schema.org",
  "@type": "TechArticle",
  "headline": "UIKit Animations with TexturePacker",
  "author": [
    {
      "@type": "Person",
      "name": "Andreas Löw"
    }
  ],
  "dateModified": "2023-09-21T00:00:00.000Z",
  "image": "/static/c632d7f4cb72b7108d54f93293a24ddd/a6312/post-image.png"
}
```
