码迷,mamicode.com
首页 > 其他好文 > 详细

Chapter 13 UIGestureRecognizer and UIMenuController

时间:2014-09-24 00:57:45      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   for   sp   art   cti   on   c   

Chapter 13 UIGestureRecognizer and UIMenuController 

 

1. A UIGestureRecognizer intercepts touches that are on their way to being handled by a view. When it recognizes a particular gesture, it sends a message to the object of your choice. There are several types of gesture recognizers built into the SDK.

 

2. A UIMenuController has a list of UIMenuItem objects and is presented in an existing view. Each item has a title (what shows up in the menu) and an action (the message it sends the first responder of the window).

 

3. There is only one UIMenuController per application. When you wish to present this instance, you fill it with menu items, give it a rectangle to present from, and set it to be visible.

 

-(void)tap:(UIGestureRecognizer*)gr

{

    NSLog(@"Recognized tap");

    

    CGPoint point = [gr locationInView:self];

    self.selectedLine = [self lineAtPoint:point];

    

    if(self.selectedLine)

    {

        // Make the target of menu item action messages

        [self becomeFirstResponder];

        // Grab the menu controller

        UIMenuController *menu = [UIMenuController sharedMenuController];

        // Create a new "delete" UIMenuItem

        UIMenuItem *deleteItem = [[UIMenuItem alloc] initWithTitle:@"delete" action:@selector(deleteLine:)];

        menu.menuItems = @[deleteItem];

        

        // Tell the menu where it should come from and show it

        [menu setTargetRect:CGRectMake(point.x, point.y, 2, 2) inView:self];

        [menu setMenuVisible:YES animated:YES];

    }

    else

    {

        [[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES];

    }

    

    [self setNeedsDisplay];

}

 

4. For a menu controller to appear, a view that responds to at least one action message in the UIMenuController’s menu items must be the first responder of the window - this is why you sent the message becomeFirstResponder to the view before setting up the menu controller. If you have a custom view class that needs to become the first responder, you must override canBecomeFirstResponder.

-(BOOL)canBecomeFirstResponder

{return YES;}

 

5. Normally, a gesture recognizer does not share the touches it intercepts. Once it has recognized its gesture, it “eat” that touch, and no other recognizer gets a chance to handle it.

 

6. gestureRecognizer: shouledRecognizeSimultaneouslyWithGestureRecognizer.  A gesture recognizer will send this message to its delegate when it recognizes its gesture but realizes that another gesture recognizer has recognized its gesture, too. If this method returns YES, the recognizer will share its touches with other gesture recognizers.

 

7. A pan gesture recognizer supports the changed state. When a finger starts to move, the pan recognizer enters the began state and sends a message to its target. While the finger moves around the screen, the recognizer transitions to the changed state and sends its action message to its target repeatedly. Finally, when the finger leaves the screen, the recognizer’s state is set to ended, and the final message is delivered to the target.

 

8. Every UIGestureRecognizer has a property cancelsTouchesInView, by default, this property is YES. This means that gesture recognizer will eat any touch it recognizes so that the view will not have a chance to handle it via the traditional UIResponder methods, like touchesBegan:withEvent:. When you set cancelsTouchesInView to NO, touches that the gesture recognizer recognizes also get delivered to the view via the UIResponder methods. This allows both the recognizer and the view’s UIResponder methods to handle the same touches.

 

9. Overall, there are seven states a recognizer can enter:

UIGestureRecognizerStatePossible

UIGestureRecognizerStateFailed

UIGestureRecognizerStateBegan

UIGestureRecognizerStateCancelled

UIGestureRecognizerStateChanged

UIGestureRecognizerStateRecognized

UIGestureRecognizerStateEnded

Most of the time, a recognizer will stay in the possible state. When a recognizer recognized its gesture, it goes into the began state. If the gesture is something that can continue, like a pan, it will go into and stay in the changed state until it ends. When any of its properties change, it sends another message to its target. When the gesture ends (typically when the user lifts the finger), it endures the ended state.

 

 

 

 

 

 

 

Chapter 13 UIGestureRecognizer and UIMenuController

标签:io   os   ar   for   sp   art   cti   on   c   

原文地址:http://www.cnblogs.com/1oo1/p/3989448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!