Detect touch in UIImageView in UIScrollView
i have images in my UIScrollview which is added in View.
plz go through my code-@interface ScrollViewController : UIScrollView <UIScrollViewDelegate>{
UIImageView *productImage;
UILabel *productName;
NSArray *productArray;
}
@property(nonatomic,retain) UIImageView *productImage;
@property(nonatomic,retain) UILabel *开发者_高级运维productName;
@property(nonatomic,retain) NSArray *productArray;
- (id)initWitProducts:(NSArray*)_data;
*.m*
- (id)initWitProducts:(NSArray *)_data
if ((self = [super init])){
productArray=[[NSArray alloc]initWithArray:_data];
[self setFrame:CGRectMake(0, 0, 320, 480)];
int countList=[self.productArray count];
self.contentSize=CGSizeMake(320, 585);
for(int i=0;i<countList;i++)
{
productImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[[productArray objectAtIndex:i]objectForKey:@"ProductImage"]]];
productImage.frame=CGRectMake(95, 35+i*125, 100, 100);
[productImage setUserInteractionEnabled:YES];
[self addSubview:productImage];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
{
if([[touches anyObject]view]==self.productImage)
NSlog(@"Image Touched");
}
This is working well but only for last image in Array
the touchesBegan is not working for other images in Array what should i add here to detect the touch on all the images(1st,2nd,...etc) of ArrayYou know all the positions of images on UIScrollview, so you can touch position on scrollView into images position in you array. And another point - you should never compare objective-c objects using "==", always use "isEqual" method instead.
That is because you keep on updating the variable productImage, change your code to read
UIImageView *imageInstance=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[[productArray objectAtIndex:i]objectForKey:@"ProductImage"]]];
imageInstance.frame=CGRectMake(95, 35+i*125, 100, 100);
[imageInstance setUserInteractionEnabled:YES];
[self addSubview:imageInstance];
精彩评论