Skip to content

Instantly share code, notes, and snippets.

@luxinyan
Last active August 29, 2015 14:05
Show Gist options
  • Save luxinyan/7ef4535164075fc83754 to your computer and use it in GitHub Desktop.
Save luxinyan/7ef4535164075fc83754 to your computer and use it in GitHub Desktop.
use implement and use a gesture
- (void)setPannableView:(UIView *)pannableView // maybe this is a setter in a Controller
{
_pannableView = pannableView;
UIPanGestureRecognizer *pangr =
[[UIPanGestureRecognizer alloc] initWithTarget:pannableView action:@selector(pan:)];
[pannableView addGestureRecognizer:pangr];
}
//implement a gesture
- (void)pan:(UIPanGestureRecognizer *)recognizer
{
if ((recognizer.state == UIGestureRecognizerStateChanged) ||
(recognizer.state == UIGestureRecognizerStateEnded)) {
CGPoint translation = [recognizer translationInView:self];
// move something in myself (I’m a UIView) by translation.x and translation.y
// for example, if I were a graph and my origin was set by an @property called origin
self.origin = CGPointMake(self.origin.x+translation.x, self.origin.y+translation.y);
[recognizer setTranslation:CGPointZero inView:self];
}
}
/******Gesture method******/
//how far the gesture move
- (CGPoint)translationInView:(UIView *)aView;
//how fast the gesture move
- (CGPoint)velocityInView:(UIView *)aView;
//reset the translation
- (void)setTranslation:(CGPoint)translation inView:(UIView *)aView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment