简体   繁体   中英

Tap gesture is not working on UIimageview

In my application i have three uiimageview which is moving randomly. on single tap on imageview it should hide. But my tapgesture is not working. on single tap it is not getting hide.

- (void)showAlert1:(UITapGestureRecognizer *)sender
{
    if (image1.tag == 1)
    {
        image1.hidden = TRUE;
    }
    else
    {
        image1.hidden = FALSE;
    }
}

- (void)showAlert2:(UITapGestureRecognizer *)sender
{
    if (image1.hidden == TRUE && image3.hidden == FALSE)
    {
        image2.hidden = TRUE;
    }
    else
    {
        image2.hidden = FALSE;
    }
}

- (void)showAlert3:(UITapGestureRecognizer *)sender
{
    if (image1.hidden == TRUE && image2.hidden == TRUE)
    {
        image3.hidden = TRUE;
    }
    else
    {
        image3.hidden = FALSE;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (image1.tag == 1)
    {
        image1.userInteractionEnabled = YES;

        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(imageAlerts];
        tap.numberOfTapsRequired = 1;
        [image1 addGestureRecognizer:tap];
    }

    if (image2.tag == 2)
    {
        image2.userInteractionEnabled = YES;

        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert2];
        tap.numberOfTapsRequired = 1;
        [image2 addGestureRecognizer:tap];
    }

    if (image3.tag == 3) 
    {
        image3.userInteractionEnabled = YES;

        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert3];
        tap.numberOfTapsRequired = 1;
        [image3 addGestureRecognizer:tap];
    }
}

Can anyone help me?

Thanks in advance

Please checkmark the userInteractionEnabled and multipleTouch in xib file if you have added image in xib

or

image.userInteractionEnabled = YES;
image.multipleTouchEnabled = YES;

in ViewDidLoad

你实现了UIGestureRecognizerDelegate并设置为self吗?

try this -

- (void)viewWillAppearBOOL:animated
{

[super viewWillAppear:animated];

if (image1.tag==1)

{

image1.userInteractionEnabled = YES;

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert1:];

tap.numberOfTapsRequired = 1;

[image1 addGestureRecognizer:tap];

}

if (image2.tag==2) 

{

image2.userInteractionEnabled = YES;

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert2:];

tap.numberOfTapsRequired = 1;

[image2 addGestureRecognizer:tap];

}
if (image3.tag==3) 

{

image3.userInteractionEnabled = YES;

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert3:];

tap.numberOfTapsRequired = 1;

[image3 addGestureRecognizer:tap];

}
}

Also it has memory leak. UIGesture is not released after it adds to image.

Try this

- (void)showAlert1:(UITapGestureRecognizer *)sender 
{     
  if (sender.state == UIGestureRecognizerStateEnded)     
  {        
     // your handling code
    if (image1.tag==1)
         image1.hidden=TRUE;
    else
         image1.hidden=FALSE;     
  }  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM