Using Image to Perform an Action instead of Button in iPhone Applciation
How can i use image to Perform an Action instead of Using Button or adding an image to Butto开发者_如何学Gon, just wanna click button and perform particular set of instructions. (Thanks in Advance)
Use a UIGestureRecogniser.
// IF your image view is called myImage
UIImageView *myImage = ...;
// Add a tap gesture recogniser
UITapGestureRecogniser *g = [[UITapGestureRecogniser alloc] initWithTarget:self action:@selector(imagePressed:)];
[myImage addGestureRecogniser:g];
[g release];
When your image is tapped, this method will get called
- (void)imagePressed:(UIGestureRecogniser *)recogniser {
NSLog(@"%@", recogniser);
}
Why don't you want to use a UIButton - it inherits from UIControl and has a lot of code that you probably don't even know exists? And it can just contain an image so it would look exactly the same?
Well, the pro about using UIButton
s is that it got all touch events built right in. You can use UIImageView
s, but you'll need to subclass them, while in most situations, a UIButton
using a background-image would just fit.
精彩评论