touches event handler for UIImageView

2019-04-30 18:57发布

I am just getting stated with iPhone development and can't seem to find the answer I am looking for what I want to do.

It seems like I should be able to programmatically create a UIImageView and then set up an event handler for it's touch functions.

in c# i would have something that looks like

Button b = new Button(); b.Click+= my handler code

right now I have this

CGRect myImageRect = CGRectMake(0.0f, 0.0f, 141.0f, 151.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];

myImage.userInteractionEnabled = YES;
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];

What do I need to do to override the touch events?

thanks

7条回答
乱世女痞
2楼-- · 2019-04-30 19:36

While it is not the complete answer to your question, i think the following solution might be better then doing the "invisible-button-workaround".. simply subclass from UIImageView and override the following method:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

 //your UIImageView has been touched :)

 //event -> "A UIEvent object representing the event to which the touches belong."

 //touches -> "A set of UITouch instances in the event represented by event that    represent the touches in the UITouchPhaseEnded phase."

}

hope this helps...

查看更多
我想做一个坏孩纸
3楼-- · 2019-04-30 19:36

I have a mainview with some icons like "about" to open an about view. In the corresponding controller (mainview) I added an event handler for touchesBegan. Moreover I need to check which view has been "touched", as there are more icons on the view. My UIImageView instance is "aboutImg" and user interaction needs to be checked (i.e. UI builder -> attributes inspector of view).

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch view] == aboutImg){
        [self ShowAbout];
    }
}

Make sure you check the "User Interactions enabled". This is the easiest solution

查看更多
我想做一个坏孩纸
4楼-- · 2019-04-30 19:47

Just use a custom UIButton with the image as background image.

查看更多
淡お忘
5楼-- · 2019-04-30 19:48

Easiest way to add an onClick event to an UIImageView or to a UIView is to add a tap gesture recognizer to it.

UIImage *myImage = [UIImage imageNamed:@"myImage.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage: myImage];

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action: @selector(onImageViewClicked)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self.myImageView addGestureRecognizer:singleTap];
[self.myImageView setUserInteractionEnabled:YES];
查看更多
倾城 Initia
6楼-- · 2019-04-30 19:50

You need nothing to do with UIImageView, You can use UIButton with type custom and add touch up inside action to it...set button.imageview setImage: i-e:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0,0,100,100); //edit for you own frame
UIImage *img = [UIImage imageNamed:@"myimage.png"];
[button setImage:img forState:UIControlStateNormal];

If you want to go with UIImageView, then you need to add gestures to your imageview. else you can subclass your UIImageView and then use touch events.

查看更多
看我几分像从前
7楼-- · 2019-04-30 19:53

I've seen other answers suggest putting an invisible button underneath the UIImageView, setting the image view to userInteractionEnabled: NO, and letting the touches pass through to the button.

查看更多
登录 后发表回答