【问题标题】:UITapGestureRecognizer not working, Objective-CUITapGestureRecognizer 不工作,Objective-C
【发布时间】:2016-03-13 04:55:45
【问题描述】:

对于第 100 万次提出这个问题,我深表歉意,我已经阅读了其他相同问题的问题,但我找不到问题的答案!

我制作了一个 UITapGestureRecognizer 无法正常工作,有人可以看看我的代码并告诉我我做错了什么吗?

-(void) formatCellForY: (CGFloat) y{

CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat cellHeight = 70;

self.cell = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"Cell"]];
[self.cell setFrame:CGRectMake(20, y, screen.size.width - 40, cellHeight)];

self.cell.backgroundColor = [UIColor grayColor];
self.cell.userInteractionEnabled = YES;


self.cell.layer.shadowColor = [UIColor blackColor].CGColor;
self.cell.layer.shadowOffset = CGSizeMake(0, 1);
self.cell.layer.shadowOpacity = 1.0;
self.cell.layer.shadowRadius = 1.0;
self.cell.layer.cornerRadius = 1.0;
self.cell.clipsToBounds = NO;

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, self.cell.frame.size.width - 40, cellHeight*(.4))];
titleLabel.numberOfLines = 32;
titleLabel.text = self.program.programName;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.userInteractionEnabled = YES;
[self.cell addSubview:titleLabel];

UILabel *explanationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, titleLabel.frame.origin.y + titleLabel.frame.size.height, titleLabel.frame.size.width, cellHeight - (titleLabel.frame.origin.y+ titleLabel.frame.size.height))];
explanationLabel.text = self.program.programDescription;
explanationLabel.numberOfLines = 3;
explanationLabel.textColor = [UIColor whiteColor];
explanationLabel.font = [UIFont systemFontOfSize:10.0];
explanationLabel.userInteractionEnabled = YES;
[self.cell addSubview:explanationLabel];

self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToInfoPage:)];
self.tap.numberOfTapsRequired = 1;
self.tap.numberOfTouchesRequired = 1;
self.tap.enabled = YES;
[self.cell addGestureRecognizer:self.tap];

NSLog(@"%@", self.tap);
}

这是我用来将单元格添加到屏幕的代码。

for (CKRecord *record in records) {
        SBHProgram *program = [SBHProgram programForRecord:record];
        SBHCell *cell = [SBHCell cellForProgram:program andY:90*i];
        i++;
        [scrollView addSubview:cell.cell];
}

【问题讨论】:

  • 您已经创建了“单元格”,但您是否已将“单元格”添加到任何子视图中?我看到你已经在单元格中添加了子视图......
  • 是的,单元格被添加到 MainViewController 的主视图中
  • @Nshweky:这适用于我的模拟器!
  • @Nshweky:您是在其父视图区域之外添加单元格吗?
  • 我不这么认为,只是一个简单的 [self.view addSubview: obj.cell];

标签: ios objective-c uitapgesturerecognizer


【解决方案1】:

您错过了将 self.cell 添加到视图。您可以看到带有文本的标签,因为您添加了 self.cell.clipsToBounds = NO;

您所要做的就是添加要查看的单元格。

[self.view addSubview:self.cell]

【讨论】:

  • 你从哪个方法调用 formatCellForY: ?
  • SBHCell 类的初始化器。
  • 您的滚动视图可能正在消耗水龙头。看看这个stackoverflow.com/questions/17701323/…delayContentTouches 应该可以解决你的问题
  • 这可能与我分配给目标的内容有关吗?我改变了它,它开始接收触摸,当它发生时就崩溃了。
【解决方案2】:

请添加

self.tap.delegate = self;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多