【问题标题】:My touchesBegan wont trigger我的 touchesBegan 不会触发
【发布时间】:2012-07-30 01:10:16
【问题描述】:

您好,我有一个问题,我几天都想不通。我有一个 touchesBegan 方法,我想用它来判断何时有人点击 UIImageView,以便它可以使用 NSTimers 上下移动不同的 UIImageView。出于某种原因,我的 TouchesBegan 无法工作,我知道这是问题所在,因为我使用 NSLogs 进行了测试,结果表明该方法没有被调用。这是我的代码,谢谢 附言如果有一个超级快速的修复,它让我看起来很愚蠢,我很抱歉我现在只开发了 6 天。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint pt = [[touches anyObject] locationInView:self.view];

    if (CGRectContainsPoint(flyImageButton.frame, pt)) {

        flying = YES;

        [playerFall invalidate];

        playerFlyUp = [NSTimer scheduledTimerWithTimeInterval:.03 target:self selector:@selector(flyUp) userInfo:nil repeats:YES];

        [[NSRunLoop mainRunLoop] addTimer:playerFlyUp forMode:NSRunLoopCommonModes];
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (flying == YES) {
        flying = NO;
        [playerFlyUp invalidate];
        [self startFallTimer];
    }
}

这是我的 .h 文件,供询问的人使用...

#import <UIKit/UIKit.h>

@interface GameViewController : UIViewController {
    IBOutlet UILabel *scoreLabel, *coinsLabel;
    IBOutlet UIImageView *flyImageButton, *playerImage, *flyingObject;
    IBOutlet UIScrollView *scroll;

    int score;
    int coins;

    BOOL flying;

    NSTimer *playerFlyUp, *playerFall, *scoreTimer, *coinsTimer;
}

@end

我也试过这样的 UITouch...

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //CGPoint pt = [[touches anyObject] locationInView:self.view];
    UITouch *touch = [[event allTouches]anyObject];

    //if (CGRectContainsPoint(flyImageButton.frame, pt)) {
    if ([touch view] == flyImageButton) {
        flying = YES;

        [playerFall invalidate];

        playerFlyUp = [NSTimer scheduledTimerWithTimeInterval:.03 target:self selector:@selector(flyUp) userInfo:nil repeats:YES];

        [[NSRunLoop mainRunLoop] addTimer:playerFlyUp forMode:NSRunLoopCommonModes];
    }
}

让我真正感到困惑的是 NSLog "Pos 1" 是如何从不触发的,无论我在哪里触摸......

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //CGPoint pt = [[touches anyObject] locationInView:self.view];
    UITouch *touch = [[event allTouches]anyObject];
    CGPoint loc = [touch locationInView:self.view];

    //This NSLog never displays leading me to think that my CGPoint isn't the issue...
    NSLog(@"Pos 1");

    if (CGRectContainsPoint(flyImageButton.frame, loc)) {
    //if ([touch view] == flyImageButton) {
        NSLog(@"Pos 2");
        flying = YES;

        [playerFall invalidate];

        playerFlyUp = [NSTimer scheduledTimerWithTimeInterval:.03 target:self selector:@selector(flyUp) userInfo:nil repeats:YES];

        [[NSRunLoop mainRunLoop] addTimer:playerFlyUp forMode:NSRunLoopCommonModes];
    }
}

【问题讨论】:

  • 我们可以看看你的.h文件吗
  • 你确定你没有什么东西吃掉你的触摸(例如你的视图顶部的按钮)?此外,如果您计划执行自定义触摸处理,则应根据苹果指南覆盖所有 4 种触摸方法。
  • UIScrollView 会干扰它吗?我有一个触发 touchesBegan 的按钮,但它应该在滚动视图上执行某些操作

标签: objective-c ios xcode touchesbegan


【解决方案1】:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
      UITouch * touch = [[event allTouches]anyObject];
}

【讨论】:

  • 那我在哪里使用触摸? (对不起,我是新手)
  • 如何使用你告诉我使用那行代码创建的 UITouch
  • 用这两行替换你的前两行代码:) 然后告诉我它是如何工作的。
  • 好的,所以你告诉我现在声明 UITouch 的对象称为“触摸”,我该如何使用“触摸”,因为目前它说它未使用并抛出黄色警告。所以我想问的是,我要改变什么来使用“触摸”以及如何将它添加到我的视图中
  • 比较项目直到发现错误,在我面前没有完整项目的情况下有点难以找到一些东西,这可能是你最好的选择。祝你好运!
【解决方案2】:
CGPoint pt = [[touches anyObject] locationInView:nameofyour_imageview];

将其更改为指向您的 UIImageview 而不是 self.view 或尝试点击手势识别器

【讨论】:

  • 对不起,你是个菜鸟,但你是怎么做到的?
  • nameofyour_imageview 在哪里输入要在其中注册触摸事件的图像视图的名称。我猜是flyImageButtonCGPoint pt = [[touches anyObject] locationInView:flyImageButton];
  • 当我这样做时会抛出一个错误:“在 UIImageView * 类型的对象上找不到属性 'CGPoint”
  • 好的,我尝试删除滚动视图,效果很好。所以滚动视图一定是有干扰的。我会调查的! :)
  • 好的,所以我 99% 确定这是我的滚动视图的分层问题。代码很好,但我弄乱了 xib 文件并删除了滚动视图修复了它。但是我需要滚动视图,我应该怎么做才能解决这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-12
相关资源
最近更新 更多