iOS开发之-Gesture Recognizer

本文介绍了手势识别器如何简化触摸事件处理,包括它们如何被附加到视图上、触发的动作消息及其实现方式。同时探讨了离散和连续手势的区别,并提供了创建和响应手势识别器的具体代码示例。

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


Preparing a Gesture Recognizer

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


Requiring a Gesture Recognizer to Fail

1. You might want a relationship between two gesture recognizers so that one can operate only if the other one fails. For example, recognizer A doesn’t begin analyzing a multitouch sequence until recognizer B fails and, conversely, if recognizer B does recognize its gesture, recognizer A never looks at the multitouch sequence. An example where you might specify this relationship is when you have a gesture recognizer for a single tap and another gesture recognizer for a double tap; the single-tap recognizer requires the double-tap recognizer to fail before it begins operating on a multitouch sequence.





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值