In addition to the beginning touch point and the last updated point, we also keep track the currently displayed color and the full array of colors that the view can rotate through. There are a number of things we need to set up in the view’s subclass, the first of which is to override the superclass’s -initWithFrame: method, After calling the super’s initWithFrame, build the color array for the view to rotate through and set the initial colorIndex to zero. Next, set the view’s backgroundColor based on that index, and then set the userInteractionEnabled to YES so that the application can receive single touch events. With touch events enabled, we need to implement all the touch methods to receive those events. The first one is -touchesBegan: withEvent.
This method records the location of the beginning touch with touchBeganPoint and sets the lastUpdatedPoint to the same location. We grab any of the touches from the set because we are configured only for single touch events, so it is irrelevant as to which touch we get. Now that the touch workflow is set, we need to wait for the next event before we can perform any action. One of three possible methods will be called, the easiest of which to implement is the -touchesCancelled:withEvent.
A cancel event occurs only when something interrupts the application. For example, this might happen when there is an incoming SMS message, phone call, or some other external event on the iPhone. As such, don’t treat a cancel as a failure but as an opportunity to reset the state. If the event is not canceled, you can send either of the following two methods next: -touchesEnded:withEvent: or -touchesMoved:withEvent:. For this application, we implement -touchesEnded:withEvent:
Using this method, we first determine the delta of the current UITouch based on the touchBeganPoint. If the x or y delta is greater than the tap threshold, the touch is treated as the end of a move and resets the state. If the touch is within the tap threshold, however, we need to change the background color of the view and cause it to throb for visual feedback. After rotating the color index and grabbing a reference for the next color, we begin an animation block. This tells the iPhone OS that all the property changes within this block should be performed together as a single animation transaction. We then set ourselves as the delegate for the animation. Because we want the transform to reverse, we need to add another transform to the delegate call. This enables us to reset the scale back to normal after the initial zoom as completed.
After setting the delegate, we give it an @selector to call back on when the animation is complete. Finally, we apply the grow transform, set the background color, and commit the animations. This now causes the view to expand by 40 percent and change its background color in a smooth animation. After this animation block is complete, we need to apply another transform with the -throbReset: context: method to undo the grow transform.