Online Book Reader

Home Category

iPhone Game Development - Chris Craft [29]

By Root 1513 0
3.6). When you release the placard, it springs back to the center and wobbles a little.

FIGURE 3.6

The MoveMe example from iPhone Dev Center is an excellent example to get you started with Multi-Touch.


Let's examine the code that makes this work. In order to begin capturing touch input, you will need to implement one or more of the following events in your UIView:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

Responding to touchesBegan

If you run the example, you will notice that when you touch the placard it grows a little and then you can drag it. This first response is captured in the touchesBegan event:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];

if ([touch view] != placardView) {

...

return;

}

CGPoint touchPoint = [touch locationInView:self];

[self animateFirstTouchAtPoint:touchPoint];

}

Notice that you do not have a single touch parameter but an NSSet of touches. The first thing this example does is distill this down to one touch within the line:

UITouch *touch = [touches anyObject];

Now a check is performed to see if the view that is responsible for reporting the touch is the placard; if it is not, the method exits before any animations are started:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];

if ([touch view] != placardView) {

...

return;

}

CGPoint touchPoint = [touch locationInView:self];

[self animateFirstTouchAtPoint:touchPoint];

}

Next, locationInView is used to convert the position of the touch into a new position that is relative to the Main View:

CGPoint touchPoint = [touch locationInView:self];

Finally, an animation is performed and you see the button grow on the screen:

[self animateFirstTouchAtPoint:touchPoint];

Responding to touchesMoved

As you drag your finger on the screen, you experience the placard sliding across the screen. This behavior is achieved by responding to the touchesMoved event:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];

if ([touch view] == placardView) {

CGPoint location = [touch locationInView:self];

placardView.center = location;

return;

}

}

Most of the setup code in touchesMoved is the same as in touchesBegan. All you need to do is reset the center of the placard to the center of the touch:

placardView.center = location;

Responding to touchesEnded

When you remove your finger from the screen, you see the placard spring back to the center of the screen. This behavior is achieved by responding to the touchesEnded event:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];

if ([touch view] == placardView) {

self.userInteractionEnabled = NO;

[self animatePlacardViewToCenter];

return;

}

}

Most of the setup code in touchesEnded is the same as in touchesBegan. After the common setup is complete, you will want to turn off any user interaction until the placard returns to its original location. This is accomplished with the following line:

self.userInteractionEnabled = NO;

Now that interaction is disabled, you can begin the animation. In our example this is accomplished by a call to the instance method animatePlacardViewToCenter:

[self animatePlacardViewToCenter];

It is important that you restore user interaction when the animation is complete. It is not shown here, but when the animation is complete, user interaction is restored. You can accomplish this by setting the value of the property userInteractionEnabled to YES, as shown below:

self.userInteractionEnabled = YES;

Responding to touchesCancelled

The touchesCancelled

Return Main Page Previous Page Next Page

®Online Book Reader