Domanda

In iPad when you put your finger outside top or bottom edge of screen and then drag it on screen a menu is revealed. How can I implement that?

È stato utile?

Soluzione

  • There is specifically a Gesture Recogniser class for this, introduced in iOS 7. It's the UIScreenEdgePanGestureRecognizer. The documentation for it is here. Check it out.

  • To test this in the simulator, just start the drag from near the edge (~15 points).

  • Also, you will have to create a gestureRecognizer for each edge. You can't OR edges together, so UIRectEdgeAll won't work.

There is a simple example here. Hope this helps!

Altri suggerimenti

Well you can do something like this, this example is the case where you want you pan gesture to work only when the user swipes 20px inside from the right hand side of the screen

First of all add the gesture to your window

- (void)addGestures {  
if (!_panGesture) {      
_panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; 
[_panGesture setDelegate:self]; 
[self.view addGestureRecognizer:_panGesture];   
}
}

After adding the check whether the touch you recieved is a pan gesture and then perform your action accordingly

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {    
CGPoint point = [touch locationInView:self.view];
if (gestureRecognizer == _panGesture) {     
return [self slideMenuForGestureRecognizer:gestureRecognizer withTouchPoint:point]; 
} 
return YES;
}

Here is how you can check whether your touch is contained in the region where you want it to be

 -(BOOL)isPointContainedWithinBezelRect:(CGPoint)point {
   CGRect leftBezelRect; 
   CGRect tempRect; 
 //this will be the width between CGRectMaxXEdge and the screen offset, thus identifying teh region
   CGFloat bezelWidth =20.0;
   CGRectDivide(self.view.bounds, &leftBezelRect, &tempRect, bezelWidth, CGRectMaxXEdge); 
   return CGRectContainsPoint(leftBezelRect, point);
   }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top