开发者

Using Image to Perform an Action instead of Button in iPhone Applciation

开发者 https://www.devze.com 2023-03-30 04:47 出处:网络
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.

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 UIButtons is that it got all touch events built right in. You can use UIImageViews, but you'll need to subclass them, while in most situations, a UIButton using a background-image would just fit.

0

精彩评论

暂无评论...
验证码 换一张
取 消