TouchBegan 并不总是响应
我检测到屏幕上有滑动,它的工作方式正是我想要的。 唯一的问题是,在测试中,我不断地一遍又一遍地滑动,至少 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用Apple的
更容易UISwipeGestureRecognizer
It is much easier to use Apple's
UISwipeGestureRecognizer