【问题标题】:touchesBegan doesn't always respondtouchesBegan 并不总是响应
【发布时间】:2011-05-04 02:11:41
【问题描述】:

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

我用 NSLog 记录了一切以找到罪魁祸首,发现在这种情况发生的几次中根本没有检测到 touchesBegan。

这是我的代码,虽然 touchesBegan 甚至没有被调用,所以代码应该无关紧要:

- (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 后最终使用的代码:

在.h中:

UIViewController <UIGestureRecognizerDelegate>

UISwipeGestureRecognizer *swipeLeft;
UISwipeGestureRecognizer *swipeRight;

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

在.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;
}

【问题讨论】:

    标签: iphone touch swipe touchesbegan


    【解决方案1】:

    使用苹果的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
    }
    

    【讨论】:

    • 很高兴它有所帮助。 Apple 的手势识别器真的打出了本垒打。让我们的生活更轻松。
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2016-04-21
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    相关资源
    最近更新 更多