I have a problem with touchesBegan event.I displayed an image in an uiimageview.Am trying to detect the touches on that image by displaying small green color points on the image when i clicked on the image.. Am not getting any errors while running but the touches event is not firing.Here is my code:
#import <UIKit/UIKit.h>
#import "Second.h"
@interface pickerExampleViewController : UIViewController <UIImagePickerControllerDelegate>{
IBOutlet UIButton *selectPic;
}
@property (nonatomic,retain) UIButton *selectPic;
-(IBAction)getpic:(id)sender;
@end
#import "pickerExampleViewController.h"
@implementation pickerExampleViewController
@synthesize selectPic;
-(IBAction)getpic:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.editing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];
}
#pragma mark imagePickerController delegate methods
-(void)imagePickerController:(UIImagePickerController *) picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
Second *secview = [[Second alloc] initWithNibName:@"Second" bundle:nil];
secview.view.backgroundColor = [UIColor blackColor];
[secview setImage:image];
[self.view addSubview:secview.view];
//[self presentModalViewController:secview animated:YES];
[secview release];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picke开发者_JAVA百科r
{
[self dismissModalViewControllerAnimated:YES];
}
@interface Second : UIViewController {
IBOutlet UIImageView *imgView;
UIImage *image1;
}
@property(nonatomic,retain) UIImageView *imgView;
-(void)setImage:(UIImage *)img;
-(IBAction)back;
@end
#import "Second.h"
#import <QuartzCore/QuartzCore.h>
@implementation Second
@synthesize imgView;
static int countoftouches,i;
CGPoint points[4];
-(void)setImage:(UIImage *)img
{
[imgView setImage:img];
countoftouches=0;
}
-(IBAction)back
{
[self.view removeFromSuperview];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//NSSet *allTouches = [event allTouches];
if ([touch view] != imgView) {
return;
}
countoftouches++;
CGPoint point = [touch locationInView:imgView];
NSLog(@"x: %f, y: %f", point.x, point.y);
CGRect frame=CGRectMake(point.x, point.y, 5, 5);
if(countoftouches<=4)
{
points[i] = frame.origin;
i++;
UIButton *btn=[[UIButton alloc]initWithFrame:frame];
[btn setBackgroundColor:[UIColor greenColor]];
[imgView addSubview:btn];
[btn release];
}
}
-(void)dealloc
{
[imgView release];
[super dealloc];
}
@end
You need to set userInteractionEnabled to YES.. On youre UIImageView that is.. So:
imgView.userInteractionEnable = YES;
精彩评论