Search code examples
iphoneobjective-cuiimageviewuipopover

UIImageView showing UIPopOver


I would like to fire up this event:

- (IBAction)profilePop:(id)sender
{
    ProfileViewController * profile = [[ProfileViewController alloc] init];
    UIImageView * temp = ((UIImageView *)sender);
    profile.uid =  [[[posts objectAtIndex:((UIImageView *)sender).tag] creator] mid];
    NSLog(@"profile id %@", profile.uid);
    UIPopoverController * profilePop  = [[UIPopoverController alloc] initWithContentViewController:profile];
    [profilePop presentPopoverFromRect:CGRectMake(temp.frame.origin.x+temp.frame.size.width, temp.frame.origin.y + temp.frame.size.height/2, profile.view.frame.size.width, profile.view.frame.size.height) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
}

when a user taps on an UIImageView. All I am trying to do is to show a popover when an UIImageView is clicked and it is shown to the right of the UIImageView. I see that UIImageView doesn't have an addAction attribute from it as it's not a subclass of UIControl. I did some research that I might probably have to use a UIButton instead. Is this true? Is there a way to do this using UIImageView so I don't have to rewrite the code again? I


Solution

  • First.

    you could get the touch on any object, which has super class as UIView.

    if you see the UIImageView in apple documentation.

    UIView : UIResponder : NSObject
    

    UIResponder has function to get the touches. So implement the below functions in your view class and detect the touches on your UIImageView .

    – touchesBegan:withEvent:
    – touchesMoved:withEvent:
    – touchesEnded:withEvent:
    – touchesCancelled:withEvent:
    

    Second:

    you could also create the UITapGestureRecognizer for UIImageView.

    Check the below blog tutorial.

    Working with UIGestureRecognizers

    EDITED:

    Use below code :

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]             initWithTarget:self action:@selector(tapped:)];
    [tapRecognizer setNumberOfTapsRequired:1];
    [tapRecognizer setDelegate:self];
    [MyImageView addGestureRecognizer:tapRecognizer];
    

    if user tap once, tapped function will be called , So you tabbed function implemntation should be look like below

     -(void)tapped:(id)sender
         {
    
           NSLog(@"See a tap gesture");
    
             ProfileViewController * profile = [[ProfileViewController alloc] init];
             UIImageView * temp = [(UIPanGestureRecognizer*)sender view];
            profile.uid =  [[[posts objectAtIndex:((UIImageView *)sender).tag] creator] mid];
            NSLog(@"profile id %@", profile.uid);
            UIPopoverController * profilePop  = [[UIPopoverController alloc] initWithContentViewController:profile];
          [profilePop presentPopoverFromRect:CGRectMake(temp.frame.origin.x+temp.frame.size.width, temp.frame.origin.y + temp.frame.size.height/2, profile.view.frame.size.width, profile.view.frame.size.height) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
        }