开发者

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 Array


You 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];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜