iPhone:设置所需的滑动手势长度
|
我想知道是否有一种简单的方法来设置最小滑动长度,即用户需要滑动以将手势识别为滑动的像素长度。
我注意到普通的滑动操作可能没有反应(例如,与在照片库中滑动照片相比)。
这是正常的方式,但是我想减少所需的滑动长度:
- (void)viewDidLoad
{
// SWIPING GESTURES:
UISwipeGestureRecognizer *swipeLeftRecognizer;
swipeLeftRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(foundLeftSwipe:)];
swipeLeftRecognizer.direction=UISwipeGestureRecognizerDirectionLeft;
//swipeRecognizer.numberOfTouchesRequired=1;
[self.view addGestureRecognizer:swipeLeftRecognizer];
[swipeLeftRecognizer release];
UISwipeGestureRecognizer *swipeRightRecognizer;
swipeRightRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(foundRightSwipe:)];
swipeRightRecognizer.direction=UISwipeGestureRecognizerDirectionRight;
//swipeRecognizer.numberOfTouchesRequired=1;
[self.view addGestureRecognizer:swipeRightRecognizer];
[swipeRightRecognizer release];
[super viewDidLoad];
}
#pragma mark -
#pragma mark Swipes
- (void)foundLeftSwipe:(UISwipeGestureRecognizer *)recognizer {
// do something
}
- (void)foundRightSwipe:(UISwipeGestureRecognizer *)recognizer {
// do something
}
我记得有一种方法可以获取像素的开始位置和结束位置,然后将两者进行比较,但是我只是想知道是否存在一种更简单的方法,即通过简单地为代码中的最小所需滑动长度定义一个值这里。
编辑:
这就是我重新编码整个过程的方式:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.view];
CGFloat deltaXX = (gestureStartPoint.x - currentPosition.x); // positive = left, negative = right
CGFloat deltaYY = (gestureStartPoint.y - currentPosition.y); // positive = up, negative = down
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); // will always be positive
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); // will always be positive
if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {
if (deltaXX > 0) {
label.text = @\"Horizontal Left swipe detected\";
[self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
}
else {
label.text = @\"Horizontal Right swipe detected\";
[self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
}
}
if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) {
if (deltaYY > 0) {
label.text = @\"Vertical up swipe detected\";
[self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
}
else {
label.text = @\"Vertical down swipe detected\";
[self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
}
}
}
没有找到相关结果
已邀请:
4 个回复
缮淳彼誊
催备南菠亨
中没有所需的距离,但这并不是您可以使用的全部。我最终用了
!它看起来像是手势手势识别器的超类,并且肯定会公开更多有用的数据。 此类中有一个名为
的方法,该方法返回CGPoint,其中x&y是整个平移手势的总距离!对于我们的要求来说,有些有用的东西!并且它以正负值表示方向,即:y = + 100->向下滑动100点,x = + 100左右滑动100点,依此类推... 我正在做的是检查用户是否已平移给定的点数(我正在寻找向下滑动的内容,所以对我来说是y> 150),然后执行我的操作... 这是一些代码摘录:
它也为您提供锅的速度:
注意:如果要获得整体平移效果,则应使用超级视图(大多数情况下为self.view)。 希望对您有所帮助:UIPanGestureRecognize类参考
玩翁文醚碱
悸翠疮武昏