Download presentation
Presentation is loading. Please wait.
1
EEC-492/693/793 iPhone Application Development
Lecture 16 Wenbing Zhao & Nigamanth Sridhar 2/22/2019 EEC492/693/793 - iPhone Application Development
2
Outline Taps, touches and gestures Assignment: Terminology UITouch
UIEvent UIResponder Apps in chapter 13 Assignment: Build the set of apps in chapter 13 2/22/2019 2/22/2019 EEC492/693/793 - iPhone Application Development EEC492/693/793 - iPhone Application Development 2
3
Taps, Touches and Gestures
Gesture: any sequence of events that happens from the time you touch the screen with one or more fingers until you lift your figures off the screen Touch: refers to a finger being placed on the screen One finger, one touch Tap: you touch the screen with a single finger and then immediately lift your finger off the screen without moving it around 2/22/2019 EEC492/693/793 - iPhone Application Development
4
EEC492/693/793 - iPhone Application Development
The Responder Chain Events get passed through the responder chain First responder: usually the object with which the user is currently interacting If a responder in the chain doesn’t handle a particular event, it passes that event up the responder chain Application Window UIApplication View View Controller superview View View Controller superview View View Controller View hierarchy Event 2/22/2019 EEC492/693/793 - iPhone Application Development
5
EEC492/693/793 - iPhone Application Development
UITouch Represents a single finger @property(nonatomic,readonly) NSTimeInterval timestamp; // indicates whether the touch began, moved, ended, or was canceled @property(nonatomic,readonly) UITouchPhase phase; @property(nonatomic,readonly) NSUInteger tapCount; @property(nonatomic,readonly,retain) UIWindow *window; @property(nonatomic,readonly,retain) UIView *view; // The view in which the touch initially occurred - (CGPoint)locationInView:(UIView *)view; - (CGPoint)previousLocationInView:(UIView *)view; Timestamp: The value of this property is the time, in seconds, since system startup the touch either originated or was last changed. You can store and compare the initial value of this attribute to subsequent timestamp values of the UITouch instance to determine the duration of the touch and, if it is being swiped, the speed of movement. Phase: The property value is a constant that indicates whether the touch began, moved, ended, or was canceled. For descriptions of possible UITouchPhase values, see “Touch Phase.” tapCount: The value of this property is an integer indicating the number of times the user tapped their fingers on a certain point within a predefined period. 2/22/2019 EEC492/693/793 - iPhone Application Development
6
EEC492/693/793 - iPhone Application Development
UIEvent A container for one or more touches @property(nonatomic,readonly) NSTimeInterval timestamp; - (NSSet *)allTouches; - (NSSet *)touchesForWindow:(UIWindow *)window; - (NSSet *)touchesForView:(UIView *)view; 2/22/2019 EEC492/693/793 - iPhone Application Development
7
EEC492/693/793 - iPhone Application Development
UIEvent - (NSSet *)allTouches; UIEvent UITouch Window A Window B View A View B View C 2/22/2019 EEC492/693/793 - iPhone Application Development
8
EEC492/693/793 - iPhone Application Development
UIEvent - (NSSet *)touchesForWindow:(UIWindow *)window; UIEvent UITouch Window A Window B View A View B View C 2/22/2019 EEC492/693/793 - iPhone Application Development
9
EEC492/693/793 - iPhone Application Development
UIEvent - (NSSet *)touchesForView:(UIView *)view; UIEvent UITouch Window A Window B View A View B View C 2/22/2019 EEC492/693/793 - iPhone Application Development
10
EEC492/693/793 - iPhone Application Development
Receiving Touches UIResponder - (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; 2/22/2019 EEC492/693/793 - iPhone Application Development
11
EEC492/693/793 - iPhone Application Development
UIResponder NSObject UIResponder UIApplication UIView UIViewController UIImageView UILabel UIControl UIControl UIScrollView UIButton UISlider UISwitch UITextField 2/22/2019 EEC492/693/793 - iPhone Application Development
12
Single & Multiple Touch Sequence
Show Stanford Slides 2/22/2019 EEC492/693/793 - iPhone Application Development
13
EEC492/693/793 - iPhone Application Development
The Touch Explore App - (void)updateLabelsFromTouches:(NSSet *)touches { NSUInteger numTaps = [[touches anyObject] tapCount]; NSString *tapsMessage = [[NSString alloc] taps detected", numTaps]; tapsLabel.text = tapsMessage; [tapsMessage release]; NSUInteger numTouches = [touches count]; NSString *touchMsg = [[NSString alloc] initWithFormat: @"%d touches detected", numTouches]; touchesLabel.text = touchMsg; [touchMsg release]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { messageLabel.text Began"; [self updateLabelsFromTouches:touches]; - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {…} - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {…} - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{…} 2/22/2019 EEC492/693/793 - iPhone Application Development
14
EEC492/693/793 - iPhone Application Development
The Swipes App - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; gestureStartPoint = [touch locationInView:self.view]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint currentPosition = [touch locationInView:self.view]; CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) { label.text swipe detected"; [self withObject:nil afterDelay:2]; else if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance){ label.text swipe detected"; [self withObject:nil afterDelay:2]; 2/22/2019 EEC492/693/793 - iPhone Application Development
15
The Swipes App: Detect Multiple Swipes
When touchesBegan:withEvent: gets notified that a gesture has begun, we save one finger’s position When we check for swipes, we loop through all the touches provided to the touchesMoved:withEvent: method, comparing each one to the saved point If the user did a multiple-finger swipe, when comparing to the saved point, at least one of the touches we get in that method will indicate a swipe If we find either a horizontal or vertical swipe, we loop through the touches again and make sure that every finger is at least the minimum distance away from the first finger’s horizontal or vertical position, depending on the type of swipe 2/22/2019 EEC492/693/793 - iPhone Application Development
16
The TapTaps App: Detecting Multiple Taps
If the user did a triple tap, there will be three separate notifications: Single tap Double tap Triple tap How can we handle the triple tap properly? [self withObject:nil afterDelay:.4]; [NSObject cancelPreviousPerformRequestsWithTarget:self object:nil]; 2/22/2019 EEC492/693/793 - iPhone Application Development
17
EEC492/693/793 - iPhone Application Development
The PinchMe App - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { if ([touches count] == 2) { NSArray *twoTouches = [touches allObjects]; UITouch *first = [twoTouches objectAtIndex:0]; UITouch *second = [twoTouches objectAtIndex:1]; CGFloat currentDistance = distanceBetweenPoints( [first locationInView:self.view], [second locationInView:self.view]); if (initialDistance == 0) initialDistance = currentDistance; else if (currentDistance - initialDistance > kMinimumPinchDelta) { label.text Pinch"; [self withObject:nil afterDelay:1.6f]; } else if (initialDistance - currentDistance > kMinimumPinchDelta) { label.text Pinch"; You need CGPointUtils.h and CGPointUtils.c 2/22/2019 EEC492/693/793 - iPhone Application Development
18
EEC492/693/793 - iPhone Application Development
The CheckPlease App - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint previousPoint = [touch previousLocationInView:self.view]; CGPoint currentPoint = [touch locationInView:self.view]; CGFloat angle = angleBetweenLines(lastPreviousPoint, lastCurrentPoint, previousPoint, currentPoint); if (angle >= kMinimumCheckMarkAngle&& angle <= kMaximumCheckMarkAngle && lineLengthSoFar > kMinimumCheckMarkLength) { label.text [self withObject:nil afterDelay:1.6]; } lineLengthSoFar += distanceBetweenPoints(previousPoint, currentPoint); lastPreviousPoint = previousPoint; lastCurrentPoint = currentPoint; You need CGPointUtils.h and CGPointUtils.c 2/22/2019 EEC492/693/793 - iPhone Application Development
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.