UIImageView for pushing a new ViewController

2019-09-09 09:00发布

In my app for a specific reason i want a UIImageView touchable so that when user taps it pushes to a new view controller. I know to do the same with UIButton. But i want a UIImageView to do this now. How will i do it? Here's my code for UIImageView

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 50, 50)];
[imageView setImage:[UIImage imageNamed:@"myImage.png"]];
 imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
[imageView release];

4条回答
Ridiculous、
2楼-- · 2019-09-09 09:44

Here is the complete code as Mat discussed about:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 50, 50)];
    [imageView setImage:[UIImage imageNamed:@"tap.png"]];
     [self.view addSubview:imageView];
    imageView.userInteractionEnabled = YES;


    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [tap setNumberOfTapsRequired:1];
    [tap setNumberOfTouchesRequired:1];
    [imageView addGestureRecognizer: tap];
    [tap release];
    [imageView release];
}

-(void) handleTap:(UITapGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateBegan) {


       MyTestViewController* viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"TestTable"];
        [self.navigationController pushViewController:viewController animated: YES];

    }
}
查看更多
3楼-- · 2019-09-09 09:58

As @Kory Sharp said you can use the UITapGestureRecognizer

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[tap setNumberOfTapsRequired:1];
[yourImageView addGestureRecognizer: tap];

Handle the method

-(void) handleTap:(UITapGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        //push your view controller
    }
}
查看更多
ら.Afraid
4楼-- · 2019-09-09 09:59

I am using a single tap gesture recognizer here

{
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 50, 50)];
     [imageView setImage:[UIImage imageNamed:@"myImage.png"]];
     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
     [tapGesture setNumberOfTapsRequired:1];
     [tapGesture setNumberOfTouchesRequired:1];

     [imageView setGestureRecognizers:[NSArray arrayWithObject:tapGesture]];
     [tapGesture release];
     imageView.userInteractionEnabled = YES;
     [self.view addSubview:imageView];
     [imageView release];
 }

-(void)imageTapped:(id)sender
{
  // Do STUFF HERE
}
查看更多
The star\"
5楼-- · 2019-09-09 10:00

While it would be easier to make this a UIbutton (as you me mentioned), you can implement a UITapGestureRecognizer for the UIImageView. You'll also need to enable user interaction on the view with the userInteractionEnabled property.

查看更多
登录 后发表回答