Graphics Reference
In-Depth Information
LISTING 13-14
-touchesMoved:withEvents: Implementation
- ( void )touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[UIView beginAnimations: nil context: NULL ];
CGPoint touchPoint = [[touches anyObject] locationInView:[ self window]];
float deltaX = touchPoint.x - lastUpdatedPoint .x;
float deltaY = touchPoint.y - lastUpdatedPoint .y;
float newCenterX = [[ self layer] position].x + deltaX;
float newCenterY = [[ self layer] position].y + deltaY;
CGPoint center = CGPointMake(newCenterX, newCenterY);
[[ self layer] setPosition:center];
lastUpdatedPoint = touchPoint;
[UIView commitAnimations];
}
This method begins an animation block that properly moves the animation. The delta of
the move is calculated based on the lastUpdatedPoint ; that delta is then applied to the
view's center. Because the frame is a derived value for UIView objects, you need to change
the center instead of the frame if you want to animate the action. If you tried to animate
the frame, the change would occur, but
it wouldn't animate because the call
from the -setFrame: to the -setCenter:
isn't part of the animation block.
After the center of the view's layer is
updated, you can update the state infor-
mation for the next move (or end/
cancel) and call and commit the
animations.
With the -touchesMoved:withEvent:
method fully implemented, you have
completed work on the sample applica-
tion. When you run the app, you can
see that a tap on the screen invokes a
color change to the view if that tap is
within the view's bounds, and a drag of
the view causes it to move in parallel to
the touch. The resulting application is
shown in Figure 13-7.
We implemented a few different anima-
tions in this section, some of which are
FIGURE 13-7
The Completed TouchMe
Application
 
Search WWH ::




Custom Search