gpt4 book ai didi

iPhone:设置所需的滑动手势长度

转载 作者:太空狗 更新时间:2023-10-30 03:58:30 24 4
gpt4 key购买 nike

我想知道是否有一种简单的方法来设置最小滑动长度,即用户需要滑动的像素长度,以便将手势识别为滑动。

我注意到正常的滑动可能 react 迟钝(例如与滑动照片库中的照片相比)。

这是正常的方式,但我想减少滑动所需的长度:

    - (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];
}


}



}

最佳答案

我遇到了完全相同的事情,我正在寻找解决方案。 UISwipeGestureRecognizer 中没有required distance 之类的东西,但这并不是您可以使用的全部。我最终使用了 UIPanGestureRecognizer!它看起来像是一个父类(super class)的滑动手势识别器,它肯定会公开更多有用的数据。

这个类中有一个名为 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:

注意:如果你想有一个整体的平移效果,你应该使用你的父 View (大多数情况下是self.view)。

希望这会有所帮助:UIPanGestureRecognize Class Reference

关于iPhone:设置所需的滑动手势长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5668258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com