Chapter 15. Simple Animations

WHAT YOU WILL LEARN IN THIS CHAPTER

  • How to use the NSTimer class to create timers that will call methods at regular time intervals

  • How to perform some simple animations using the NSTimer class

  • How to perform affine transformation on the ImageView

  • How to animate a series of images using the ImageView

Up to this point, the applications you have written have all made use of the standard views provided by the iPhone SDK. As Apple has reiterated, the iPhone and iPod Touch are more than just phones. The iPhone is also a music player and, more important for this chapter, the iPhone is also a gaming platform.

In this chapter, you can have some fun and create something visual. You learn how to perform some simple animations using a timer object and then perform some transformations on a view. Although it is beyond the scope of this book to show you how to create animations using OpenGL, this chapter does show you some interesting techniques that you can use to make your applications come alive!

USING THE NSTIMER CLASS

One of the easiest ways to get started with animation is to use the NSTimer class. The NSTimer class creates timer objects, which allow you to call a method at a regular time intervals. Using an NSTimer object, you can update an image at regular time intervals, thereby creating an impression that it is being animated.

In the following Try It Out, you learn how to display a bouncing ball on the screen using the NSTimer class. When the ball touches the sides of the screen, it bounces off in the opposite direction. You also learn how to control the frequency with which the ball animates. You need to download the code files indicated here for this and other Try It Out features within this chapter.

Note

After an NSTimer object is started, you cannot change its firing interval. Therefore, the only way to change the interval is to invalidate the current one and create a new NSTimer object.

Animating the Visual Change

You may have noticed that as you move the slider towards the right, the animation slows and the animation of the tennis ball becomes abrupt.

To make the animation smoother, you can animate the visual changes caused by setting the center property of the view within an animation block. The start of the animation block is defined by the beginAnimations:context: class method of the UIView class:

[UIView beginAnimations:@"my_own_animation" context:nil];
        imageView.center = CGPointMake(
                               imageView.center.x + position.x,
                               imageView.center.y + position.y);
 [UIView commitAnimations];

To end an animation block, you call the commitAnimations class method of the UIView class. The preceding code tries to animate the ImageView when it moves from one position to another (see Figure 15-6). This results in a much smoother animation than before.

Figure 15-6

Figure 15.6. Figure 15-6

TRANSFORMING VIEWS

The previous section shows how you can use the NSTimer class to simulate some simple animation by constantly changing the position of the ImageView. Besides repositioning the view, you can use the transformation techniques supported by the iPhone SDK to achieve the same effect.

Transforms are defined in Core Graphics, and the iPhone SDK supports standard affine 2D transforms. You can use the iPhone SDK to perform the following affine 2D transforms:

Note

An affine transformation is a linear transformation that preserves co-linearity and ratio of distances. This means that all the points lying on a line initially will remain in a line after the transformation, with their respective ratio of distance between each other maintained.

  • Translation — moves the origin of the view by the amount specified using the x and y axes

  • Rotation — moves the view by the angle specified

  • Scaling — changes the scale of the view by the x and y factors specified

Figure 15-7 shows the effects of the various transformations just described.

Figure 15-7

Figure 15.7. Figure 15-7

Translation

To perform an affine transform on a view, simply use its transform property. Recall that in the previous example, you set the new position of the view through its center property:

imageView.center = CGPointMake(
                           imageView.center.x + position.x,
                           imageView.center.y + position.y);

Using 2D transformation, you can use its transform property and set it to a CGAffineTransform data structure returned by the CGAffineTransformMakeTranslation() function, like this:

//---in the AnimationviewController.h file---
CGPoint position;
CGPoint translation;


//---in the viewDidLoad method---
position = CGPointMake(12.0,4.0);
translation = CGPointMake(0.0,0.0);


-(void) onTimer {
    imageView.transform = CGAffineTransformMakeTranslation(
        translation.x, translation.y);

    translation.x = translation.x + position.x;
    translation.y = translation.y + position.y;

    if (imageView.center.x + translation.x > 320 - ballRadius ||
        imageView.center.x + translation.x < ballRadius)
        position.x = -position.x;

    if (imageView.center.y + translation.y > 460 - ballRadius ||
        imageView.center.y + translation.y < ballRadius)
        position.y = -position.y;
}

The CGAffineTransformMakeTranslation() function takes in two arguments — the value to move for the x-axis and the value to move for the y-axis.

The preceding code achieves the same effect as setting the center property of ImageView.

Rotation

The rotation transformation allows you to rotate a view using the angle you specified. In the following Try It Out, you modify the code from the previous example so that the tennis ball can be rotated as it bounces across the screen.

Scaling

For scaling of views, you use the CGAffineTransformMakeScale() function to return a CGAffineTransform data structure and set it to the transform property of the view:

imageView.transform = CGAffineTransformMakeScale(angle,angle);

The CGAffineTransformMakeScale() function takes in two arguments: the factor to scale for the x-axis and the factor to scale for the y-axis.

If you modify the previous example with the preceding statement, the tennis ball gets bigger as it bounces on the screen (see Figure 15-8). It then resets back to its original size and grows again.

Figure 15-8

Figure 15.8. Figure 15-8

ANIMATING A SERIES OF IMAGES

So far, you have seen that you can use the ImageView view to display a static image. In addition, you can use it to display a series of images and then alternate between them.

The following Try It Out shows how this is done using the ImageView.

SUMMARY

In this chapter, you have seen the usefulness of the NSTimer class and how it can help you perform some simple animations. You have also learned about the various affine transformations supported by the iPhone SDK. Last, you learned about the ability of the ImageView to animate a series of images at a regular time interval.

EXERCISES

  1. Name the three affine transformations supported by the iPhone SDK.

  2. How do you pause an NSTimer object and then make it continue?

  3. What is the use of enclosing your block of code with the beginAnimations and commitAnimations methods of the UIView class?

    [UIView beginAnimations:@"some_text" context:nil];
        //---code to effect visual change---
    [UIView commitAnimations];
  • WHAT YOU HAVE LEARNED IN THIS CHAPTER

TOPIC

KEY CONCEPTS

Using the NSTimer object to create timers

Create a timer object that will call the onTimer method every half-second:

Timer = [NSTimer scheduledTimerWithTimeInterval:0.5
            target:self
            selector:@selector(onTimer)
            userInfo:nil
            repeats:YES];

Stopping the NSTimer object

[timer invalidate];

Animating visual changes

[UIView beginAnimations:@"some_text" context:nil];
//---code to effect visual change---
[UIView commitAnimations];

Performing affine transformations

Use the transform property of the view.

Translation

Use the CGAffineTransformMakeTranslation() function to return a CGAffineTransform data structure and set it to the transform property.

Rotation

Use the CGAffineTransformMakeRotation() function to return a CGAffineTransform data structure and set it to the transform property.

Scaling

Use the CGAffineTransformMakeScale() function to return a CGAffineTransform data structure and set it to the transform property.

Animating a series of images using ImageView

Set the animationImages property to an array containing UIImage objects.

Set the animationDuration property.

Set the animationRepeatCount property.

Call the startAnimating method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset