【问题标题】:objective c UIImage click event is not firing目标c UIImage点击事件未触发
【发布时间】:2018-04-16 06:56:41
【问题描述】:

我的图片不是点击表

-(void)makeBlockAction{
   blocksArr = [NSMutableArray new];
}

我的事件功能是

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch* myTouch = [[touches allObjects] objectAtIndex:0];
    NSLog(@"test2");
    if ( [ blocksArr containsObject: myTouch.view ])
    {
        myTouch.view.alpha = 0;
        NSLog(@"test");
    }
}

【问题讨论】:

  • User Interaction 是否启用?
  • -(void)makeBlockAction{ CGPoint newCen = CGPointMake(xCen, yCen); CGRect blockFrame = CGRectMake(0, 0, blockWidth, blockWidth); UIImageView* block= [[UIImageView alloc] initWithFrame:blockFrame]; NSString* imgName = [NSString stringWithFormat:@"unr1.png"]; block.image= [UIImage imageNamed:imgName]; block.center = newCen; block.userInteractionEnabled = YES; [blocksArr addObject: 块]; [_gameView addSubview:block]; xCen += 块宽度; }

标签: ios objective-c uiimage


【解决方案1】:

有更好的方法来解决这个问题,尝试将 UITapGestureRecognizer 添加到 UIImageView。这样你就不需要使用 touchesEnded 函数来检查所有的触摸了。

另外,请确保您已将 UIImageView 上的 userInteractionEnabled 设置为 YES,并且您的 UIImageView 不在任何其他可能阻止检测到点击的子视图下方。

示例

- (void)viewDidLoad
{
  [super viewDidLoad];

  UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewTapped:)];
  [self.myImageView addGestureRecognizer:tapGestureRecognizer];

  self.myImageView.userInteractionEnabled = YES;
  [self.view bringSubviewToFront: self.myImageView];
}

- (void)imageViewTapped:(UITapGestureRecognizer *)tapGestureRecognizer
{
    //Do what you need to do.
}

【讨论】:

  • @ZouhairMouhsine 你确定你的 UIImageView 没有隐藏在任何其他子视图下吗?我已经更新了我的答案
  • GOOOOD 正在工作(y),我有 30 张图片如何保存 id 点击图片?
  • @ZouhairMouhsine 太好了!很高兴我能帮助你。如果它解决了您的问题,您能否将我的答案标记为正确?另外,您想在点击图片时保存图片的 ID 吗?
  • 标记答案正确,因为这是新程序员的常见错误,应标记为“正确”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多