Gesture Recognizers
Gesture Recognizers Simplify Event Handling

Gestures Recognizers Are Attached to a View
1. To detect its gestures, a gesture recognizer must be attached to the view that a user is touching. This view is known as the hit-tested view. Delivery of events initially follows the usual path: from operating system to the application object to the window object representing the window in which the touches are occurring.

2. Thus gesture recognizers act as observers of touch objects sent to their attached view or view hierarchy. However, they are not part of that view hierarchy and do not participate in the responder chain.
3. For some gestures, the locationInView: and the locationOfTouch:inView: methods of UIGestureRecognizer enable clients to find the location of gestures or specific touches in the attached view or its subviews.
Gestures Trigger Action Messages
When a gesture recognizer recognizes its gesture, it sends one or more action messages to one or more targets.
Discrete Gestures and Continuous Gestures

Implementing Gesture Recognition
UITapGestureRecognizer *doubleFingerTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleDoubleDoubleTap:)];1. The action methods for handling gestures—and the selector for identifying them—are expected to conform to one of two signatures:
- (void)handleGesture
- (void)handleGesture:(UIGestureRecognizer *)sender
2. UIView methods:
addGestureRecognizer:
gestureRecognizers property
removeGestureRecognizer:
- (void)createGestureRecognizers {
UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSingleDoubleTap:)];
singleFingerDTap.numberOfTapsRequired = 2;
[self.theView addGestureRecognizer:singleFingerDTap];
[singleFingerDTap release];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(handlePanGesture:)];
[self.theView addGestureRecognizer:panGesture];
[panGesture release];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(handlePinchGesture:)];
[self.theView addGestureRecognizer:pinchGesture];
[pinchGesture release];
}3. You may also add additional targets and actions to a gesture recognizer using the addTarget:action: method of UIGestureRecognizer.
Responding to Gestures
- (IBAction)handlePinchGesture:(UIGestureRecognizer *)sender {
CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
self.view.transform = CGAffineTransformMakeScale(factor, factor);
}
- (IBAction)handlePanGesture:(UIPanGestureRecognizer *)sender {
CGPoint translate = [sender translationInView:self.view];
CGRect newFrame = currentImageFrame;
newFrame.origin.x += translate.x;
newFrame.origin.y += translate.y;
sender.view.frame = newFrame;
if (sender.state == UIGestureRecognizerStateEnded)
currentImageFrame = newFrame;
}
- (IBAction)handleSingleDoubleTap:(UIGestureRecognizer *)sender {
CGPoint tapPoint = [sender locationInView:sender.view.superview];
[UIView beginAnimations:nil context:NULL];
sender.view.center = tapPoint;
[UIView commitAnimations];
}Interacting with Other Gesture Recognizers
本文介绍了手势识别器如何简化触摸事件处理,包括它们如何被附加到视图上、触发的动作消息及其实现方式。同时探讨了离散和连续手势的区别,并提供了创建和响应手势识别器的具体代码示例。
2209

被折叠的 条评论
为什么被折叠?



