TouchBegan 并不总是响应

发布于 2024-11-05 05:59:40 字数 2719 浏览 1 评论 0原文

我检测到屏幕上有滑动,它的工作方式正是我想要的。 唯一的问题是,在测试中,我不断地一遍又一遍地滑动,至少 90% 或更多的时间它有响应,但时不时地没有响应。

我对所有内容进行了 NSLog 查找罪魁祸首,发现在发生这种情况的几次中根本没有检测到 TouchBegan。

这是我的代码,尽管 TouchBegan 甚至没有被调用,所以代码应该不重要:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touches began!");
    UITouch *touch = [touches anyObject];
    CGPoint thisTouch = [touch locationInView:self.view];

    touchstartedX = thisTouch.x;
    touchstartedY = thisTouch.y;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    CGPoint p = [[touches anyObject] locationInView:self.view];

    if ((abs(p.y - touchstartedY)) < 120) {
        if ((p.x - touchstartedX) > 10) {
            [self goPrevious];
        } else if ((p.x - touchstartedX) < -10) {
            [self goNext];
        } 
    } else { NSLog(@"too much Y"); }
} 

有什么想法吗?

谢谢!

* 使用解决方案进行编辑* 这是我根据 dredful 的建议探索 UISwipeGestureRecognizer 之后最终使用的代码:

in .h:

UIViewController <UIGestureRecognizerDelegate>

UISwipeGestureRecognizer *swipeLeft;
UISwipeGestureRecognizer *swipeRight;

@property (nonatomic, retain) UISwipeGestureRecognizer *swipeLeft;
@property (nonatomic, retain) UISwipeGestureRecognizer *swipeRight;

in .m:

@synthesize swipeLeft, swipeRight;

UIGestureRecognizer *recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    self.swipeLeft = (UISwipeGestureRecognizer *)recognizer;
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    self.swipeLeft = (UISwipeGestureRecognizer *)recognizer;
    [recognizer release];

    recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    self.swipeRight = (UISwipeGestureRecognizer *)recognizer;
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight; 
    self.swipeRight = (UISwipeGestureRecognizer *)recognizer;
    [recognizer release];

    [self.view addGestureRecognizer:swipeLeft];    
    [self.view addGestureRecognizer:swipeRight];

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {

    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        [self goNext];
    } else {
        [self goPrevious];
    }
}

- (void)dealloc
{
    [swipeLeft release];
    [swipeRight release];
    [super dealloc];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.swipeLeft = nil;
    self.swipeRight = nil;
}

I'm detecting a swipe on the screen, and it works exactly the way I want it to.
The only thing is, in testing, I keep swiping over and over and over again and at least 90% of the time or more it is responding, but every now and again there is no response.

I NSLog'd everything to find the culprit and found that touchesBegan isn't getting detected at all on the few times that this happens.

Here's my code, although touchesBegan isn't even getting called so the code shouldn't matter:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touches began!");
    UITouch *touch = [touches anyObject];
    CGPoint thisTouch = [touch locationInView:self.view];

    touchstartedX = thisTouch.x;
    touchstartedY = thisTouch.y;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    CGPoint p = [[touches anyObject] locationInView:self.view];

    if ((abs(p.y - touchstartedY)) < 120) {
        if ((p.x - touchstartedX) > 10) {
            [self goPrevious];
        } else if ((p.x - touchstartedX) < -10) {
            [self goNext];
        } 
    } else { NSLog(@"too much Y"); }
} 

Any ideas?

Thanks!

* EDIT WITH SOLUTION *
Here's the code I ended up using after exploring UISwipeGestureRecognizer on dredful's suggestion:

in .h:

UIViewController <UIGestureRecognizerDelegate>

UISwipeGestureRecognizer *swipeLeft;
UISwipeGestureRecognizer *swipeRight;

@property (nonatomic, retain) UISwipeGestureRecognizer *swipeLeft;
@property (nonatomic, retain) UISwipeGestureRecognizer *swipeRight;

in .m:

@synthesize swipeLeft, swipeRight;

UIGestureRecognizer *recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    self.swipeLeft = (UISwipeGestureRecognizer *)recognizer;
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    self.swipeLeft = (UISwipeGestureRecognizer *)recognizer;
    [recognizer release];

    recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    self.swipeRight = (UISwipeGestureRecognizer *)recognizer;
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight; 
    self.swipeRight = (UISwipeGestureRecognizer *)recognizer;
    [recognizer release];

    [self.view addGestureRecognizer:swipeLeft];    
    [self.view addGestureRecognizer:swipeRight];

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {

    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        [self goNext];
    } else {
        [self goPrevious];
    }
}

- (void)dealloc
{
    [swipeLeft release];
    [swipeRight release];
    [super dealloc];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.swipeLeft = nil;
    self.swipeRight = nil;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的轨迹 2024-11-12 05:59:40

使用Apple的 更容易UISwipeGestureRecognizer

// add Swipe Left
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
[swipeLeft release];

// add Swipe Right
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
[swipeRight release];


- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
    //do something
}

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer {
    //do something
}

It is much easier to use Apple's UISwipeGestureRecognizer

// add Swipe Left
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
[swipeLeft release];

// add Swipe Right
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
[swipeRight release];


- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
    //do something
}

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer {
    //do something
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文