Chapter 7

Responding to User Interaction

What You Will Learn In This Chapter:

  • Learning the basics of the iOS event architecture
  • Handling touch and multitouch events
  • Building a game with touch event handling
  • Responding to motion of the device
  • Recognizing gestures

Probably the most important part of a game is user interaction. If the player cannot interact with the game, he has no role in the outcome. It is difficult to keep a player engaged in a game if he cannot interact with it. In this chapter, you will learn a few different ways that a player can interact with your games.

Events in iOS

The first thing that you need to understand is the basic concept of user interaction in iOS. That means understanding events. When a user touches a view, the iOS operating system generates events, represented by the UIEvent object. Multitouch events occur when a player touches a view in your program with one or more fingers. You work with multitouch events by using classes in the UIKit API.

Another type of event is the motion event. Motion events happen when the iOS device moves. You can detect these events by using the accelerometer available in the iPhone and iPad. In this chapter you will learn about handling both multitouch and motion events.

The Event Architecture

When a player touches the screen in an iOS game, the UIKit framework begins a process to determine which view should handle the event. Consider the view hierarchy in Figure 7.1. This game has its basic view, in yellow, configured ...

Get Beginning iOS Game Development now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.