-1

my image is not click table

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

and my function for event is

-(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");
    }
}
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
Grenoblois
  • 503
  • 1
  • 5
  • 19
  • Is `User Interaction` enabled? – DonMag Nov 03 '17 at 14:05
  • -(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: block]; [_gameView addSubview:block]; xCen += blockWidth; } – Grenoblois Nov 03 '17 at 14:11
  • Possible duplicate of [tap gesture not recognized on uiimageview](https://stackoverflow.com/questions/19218080/tap-gesture-not-recognized-on-uiimageview) – Jayesh Thanki Nov 03 '17 at 14:13

1 Answers1

0

There are better ways to go about this, try adding a UITapGestureRecognizer to the UIImageView. That way you won't need to use the touchesEnded function and check all the touches.

Also, make sure you have set userInteractionEnabled on your UIImageView to YES and that your UIImageView is not underneath any other subviews that may be preventing the tap being detected.

EXAMPLE

- (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.
}
Rhuari Glen
  • 497
  • 2
  • 5
  • @ZouhairMouhsine Have you made sure that your UIImageView is not hidden under any other subviews? I have updated my answer – Rhuari Glen Nov 03 '17 at 14:22
  • GOOOOD is working (y) , i have 30 image how to save id the image clicking ? – Grenoblois Nov 03 '17 at 14:27
  • @ZouhairMouhsine Great! Glad I could help. Could you mark my answer as correct if it solved your issue? Also, are you wanting to save the ID of the image when it has been tapped? – Rhuari Glen Nov 03 '17 at 14:31
  • Marked answer correct, since this is a common mistake to new programmers and should be marked as „right“. – Anticro Nov 03 '17 at 20:59