n.e*_*ind 4 iphone cocoa-touch objective-c swipe
我想知道是否有一种简单的方法来设置最小滑动长度,即用户需要滑动的像素长度,以便将手势识别为滑动.
我注意到普通滑动可能反应迟钝(例如,与在照片库中滑动照片相比).
这是正常的方法,但我想减少滑动所需的长度:
- (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
}
Run Code Online (Sandbox Code Playgroud)
我记得有一种方法可以获得像素的起始位置和结束位置然后比较两者,但只是想知道是否有更简单的方法,即只需在代码中定义一个最小所需滑动长度的值这里.
编辑:
这是我记录整个事情的方式:
- (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];
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用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
}
Run Code Online (Sandbox Code Playgroud)
我面对同样的事情,我一直在寻找解决方案.没有所需的距离,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();
}
}
Run Code Online (Sandbox Code Playgroud)
它还为您提供平移速度: velocityInView:
注意:如果要获得整体平移效果,则应使用superview(大多数情况下为self.view).
希望这会有所帮助:UIPanGestureRecognize Class Reference