Chapter 18
|
Modified: |
Disclosure
First example inspired by one from Created by iPhone SDK Articles on 9/21/08, www.iPhoneSDKArticles.com
Second example based on one from The iPhone Developer's Cookbook by Erica Sadun, Addison Wesley ISBN-13 978-0-321-55545-8
Downloads
MultiTap application.
Drag Views application
Overview
IB provides GUI objects (e.g. buttons) that trigger events (e.g. Touch up inside). A more general GUI can respond to events such as the beginning or ending of a touch.
Our first example, to introduce tapping, is to count the number of taps and display the location.
The second example is a set of 16 drag-able image rectangles.
Example 1
ViewController can receive taps so create an iPhone View-based application.
Only the ViewController.m and .h files need be edited from the default generated.
UIViewController methods are over-ridden to receive tap events, the two of interest here are:
-touchesBegan: which is called when a tap began, and -touchesEnded: which is called when a tap ended.
Both are passed an UIEvent object which contains a set of UITouch objects representing all touches associated with an event (represented by the receiver).
NSSet *allTouches = [event allTouches] returns the set of UITouch objects. [[allTouches allObjects] objectAtIndex:0] is the first UITouch object in an array that corresponds to the set.
An UITouch object documentation notes the following tasks:
Getting the Location of Touches – locationInView: – previousLocationInView: view property window property Getting Touch Attributes tapCount property timestamp property phase property Getting a Touch Object’s Gesture Recognizers gestureRecognizers propertyOn a tap event, the example sends a tapcount message to return the number of taps for the event and the view property to retrieve the <x,y> position on the screen tapped. <0,0> is the upper-left corner of the screen.
The tap <x,y> location is returned by the message:
CGPoint pt = [touch locationInView: touch.view];
#import <UIKit/UIKit.h>
@interface MutiTapViewController : UIViewController {
IBOutlet UILabel *tapBeganLabel, *tapEndedLabel;
}
@end
|
#import "MutiTapViewController.h"
@implementation MutiTapViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches]; // Get all the touches.
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
CGPoint pt = [touch locationInView: touch.view]; // Retrieve touch point
NSString *taps = [[NSString alloc]
initWithFormat: @"%d <%4.0f, %4.0f> ", [touch tapCount], pt.x, pt.y];
[tapBeganLabel setText: taps];
[taps release];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches]; // Get all the touches.
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
NSString *taps = [[NSString alloc] initWithFormat: @"%d", [touch tapCount]];
[tapEndedLabel setText: taps];
[taps release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[super dealloc];
}
@end
|
Example 2 - Drag Multiple Views
Here, multiple images (views) are added as subviews to the ViewController view. Each is independent of the other, receiving tap events, etc.
Importantly for the example are the:
- property location of a view, a <x,y> value, which provides the view's current screen location.
- -touchesMoved: method called when one or more fingers move within a view.
Note that each of the multiple views responds to touch events independently.
On a real device, one can drag multiple views at a time, depending on size and how dexterous your fingers.
In this example, the UIViewController touches are ignored.
The location returned is relative coordinates of the view.
UIResponder
Defines an interface for objects that respond to and handle events.
The superclass of UIApplication, UIView and its subclasses (which include UIWindow). Instances of these classes are sometimes referred to as responder objects or, simply, responders.
Two methods are over-ridden in the following example:
-touchesBegan:withEvent: Tells the receiver when one or more fingers touch down in a view or window.
-touchesMoved:withEvent: Tells the receiver when one or more fingers associated with an event move within a view or window.
UIImageView - Inherits from UIView : UIResponder : NSObject
Each image displayed will be a UIImageView object, which responds to tap events, etc.
An image view object provides a view-based container for displaying either a single image or for animating a series of images.
For animating the images, the UIImageView class provides controls to set the duration and frequency of the animation.
Dragging is implemented by:
- -touchesBegan: stores the original touch point <x,y> location. The 64x64 pixel view has its origin at <0,0>, the upper-left of the view.
- -touchesMoved: receives the new <x,y> point within the view.
- Compute the difference between the original and new point.
- Change the view location on the screen by that difference, implementing the dragging of the view in the direction of the move.
Note:
- touch coordinate is relative to the image, <0,0> to <63,63>
- the frame coordinate is absolute, <0,0> to <319,479>
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint pt = [[touches anyObject] locationInView:self];
CGRect frame = [self frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[self setFrame:frame];
}
UIImage - Inherits from UIView : UIResponder : NSObject
A UIImage object is a high-level way to display image data. You can create images from files, from Quartz image objects, or from raw image data you receive.
The UIImage class also offers several options for drawing images to the current graphics context using different blend modes and opacity values.
-imageNamed: Returns the image object associated with the specified filename. Here, the filename is in the app bundle.
UIViewController - Inherits fom UIResponder : NSObject
Note that IB is not used to create the view displayed, instead a view is created programmatically by:
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = contentView;65x65 pixel views are placed at random screen locations by:
CGRect dragRect = CGRectMake(0.0f, 0.0f, 64.0f, 64.0f);
dragRect.origin = randomPoint();
#import <UIKit/UIKit.h>
} } @end
|