问题描述
|
我想知道是否有一种简单的方法来设置最小滑动长度,即用户需要滑动以将手势识别为滑动的像素长度。
我注意到普通的滑动操作可能没有反应(例如,与在照片库中滑动照片相比)。
这是正常的方式,但是我想减少所需的滑动长度:
- (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];
}
}
}
解决方法
请参阅此文档http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html
您可以使用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
}
,我正面临着完全相同的事情,我正在寻找解决方案。 UISwipeGestureRecognizer
中没有所需的距离,但这并不是您可以使用的全部。我最终用了UIPanGestureRecognizer
!它看起来像是手势手势识别器的超类,并且肯定会公开更多有用的数据。
此类中有一个名为translationInView:(UIView *)aView
的方法,该方法返回CGPoint,其中x&y是整个平移手势的总距离!对于我们的要求来说,有些有用的东西!并且它以正负值表示方向,即:y = + 100->向下滑动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)。
希望对您有所帮助:UIPanGestureRecognize类参考
,看看这个问题:UISwipeGestureRecognizer滑动长度
,斯威夫特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)))
}