我正在为我的应用程序添加滑动手势识别器
- (void)createGestureRecognizers
{
//adding swipe up gesture
UISwipeGestureRecognizer *swipeUpGesture= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUpGesture:)];
[swipeUpGesture setDirection:UISwipeGestureRecognizerDirectionUp];
[self.view addGestureRecognizer:swipeUpGesture];
[swipeUpGesture release];
}
和处理滑动事件的方法:
-(IBAction)handleSwipeUpGesture:(UISwipeGestureRecognizer *)sender
{
NSLog(@"handleSwipeUpGesture: called");
}
如何计算这里的偏移量?移动视图?
UIGestureRecognizer的抽象超类有以下方法
- (CGPoint)locationInView:(UIView *)view
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView *)view
可以让你知道手势在视图中的位置,但这是一个离散的手势识别器(它会在给定的"平移"或"偏移"时触发,无论你想叫它什么,你都无法控制)。这听起来像你正在寻找连续控制,为此,你需要一个UIPanGestureRecognizer,它具有以下方法(它为你做翻译计算)
- (CGPoint)translationInView:(UIView *)view
- (void)setTranslation:(CGPoint)translation inView:(UIView *)view
- (CGPoint)velocityInView:(UIView *)view
当手势不断展开时,您将获得快速的五次回调。
UISwipeGestureRecognizer用于检测一个离散的滑动手势-它只在滑动完成后触发你的动作一次-所以如果你询问手指移动的偏移量或距离,你可能想看看创建一个UIGestureRecognizer的子类或使用UIPanGestureRecognizer来获得连续的手势信息。不确定你到底想要做什么,但一个UIScrollView可能也是在顺序…
查看手势识别器的apple文档