iPhone SDK: Interact with Images
I was making an app for the iPhone and part of开发者_运维问答 what I want to do is that whenever the user touches a certain spot (let's say a circle), the app should react and do something.
How do I pinpoint that one spot as a button? Or is there a different way to do this? I have a UIScrollView with a subview of a UIImageView.
Thanks in advance!
Guy
You can learn how to handle touch from the Touches sample.
In particular, handling a simple touch happens in instances of the UIResponder
class. Since UIView
inherits from UIResponder
, any view is capable of responding to the touch messages. Thus, in order to handle touch, you just need to create a class that inherits from the particular view (UIScrollView
in your case) and overwrite the touch invent you are interested; more specifically, for your scenario you need to handle the touchesEnded:withEvent:
.
Easiest way to do this is add some transparent UIButton instances on UIScrollView, set actions and do something in each button.
for example,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch
...
UIButton* someButton = [[[UIButton alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 100.0f, 100.0f)] autorelease];
[someButton addTarget:self action:@selector(doSomething) forControlEvents:UIControlEventTouchDown];
[someUIScrollView addSubview:someButton];
...
}
- (void)doSomething {
NSLog(@"Do something");
}
You should write some codes for judging whether touched point is inside or not, if you want to create non-rect spot (such as circle). But considering finger size and user recognition, I think you don't have to implement a strict judgement.
精彩评论