我想知道是否有一种简单的方法来设置最小滑动长度,即用户需要滑动的像素长度,以便将手势识别为滑动。
我注意到正常的滑动操作非常无响应(例如,与在照片库中滑动照片相比)。
这是正常的方法,但我想减少滑动所需的长度:
- (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];
}
}
}
答案 0 :(得分:5)
您可以使用touchesBegan,touchesMoved和touchesEnded方法查找滑动的开始/结束位置。通过查找开始和结束之间的差值,您可以获得滑动长度。
也许是这样的。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:self.view];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint gestureEndPoint = [touch locationInView:self.view];
// compare gestureStartPoint and gestureEndPoint to determine swipe length
}
答案 1 :(得分:5)
我面对同样的事情,我一直在寻找解决方案。 UISwipeGestureRecognizer
中没有所需距离这样的内容,但并非所有内容都可以使用。我最终使用了 UIPanGestureRecognizer
!它看起来像一个超级的滑动手势识别器,它肯定会暴露更多有用的数据。
此类中有一种名为translationInView:(UIView *)aView
的方法可返回 CGPoint ,其中 x &amp; y 是整个平移手势的总距离!对我们的要求非常有用的东西!并且它通过正值和负值来尊重方向,意思是: y = + 100 - &gt;向上滑动100分,x = + 100滑动从左到右100分等等......
我在做什么是检查用户是否已针对给定的点数进行了平移(我正在寻找像向下滑动的东西,所以对我来说这是 y> 150 )然后做我的事......
这里有一些代码摘录:
-(void)viewDidLoad
{
UIPanGestureRecoginzer *panDown=[[UIPanGestureRecoginzer alloc] initWithTarget:self action:@selector(swipedDown:)];
[self.view addGestureRecognizer:panDown];
}
.
.
.
-(void)swipedDown:(UIPanGestureRecoginzer *)recognizer
{
CGPoint panned=[recognizer translationInView:self.view];
if(panned.y>150){
//swiped down for 150 points
doSomething();
}
}
它还为您提供平移速度:velocityInView:
注意:如果您希望获得整体平移效果,则应使用超级视图(大多数情况下为self.view)。
答案 2 :(得分:1)
答案 3 :(得分:0)
斯威夫特3:
var gestureStartPoint: CGPoint = .zero
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
gestureStartPoint = touch!.location(in: self.view)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let gestureEndPoint = touch!.location(in: self.view)
let dist = distance(gestureStartPoint, gestureEndPoint)
print(dist)
}
func distance(_ a: CGPoint, _ b: CGPoint) -> CGFloat {
let xDist = a.x - b.x
let yDist = a.y - b.y
return CGFloat(sqrt((xDist * xDist) + (yDist * yDist)))
}