mAc
mAc

Reputation: 2514

adding scrollView to my UIImageView and Tap Recognizer when i click on image

I wanted to add Scroll view to my UIimage views which are created dynamically and when i click on that images i want some action to be performed but it is not working through my code.Kindly help me...

Code :-


#import <UIKit/UIKit.h>

@class AppDelegate_iPhone,Litofinter,ParsingViewController;

@interface FirstViewController : UIViewController {

    NSMutableArray *array;
    NSString *logoString;
    AppDelegate_iPhone *appDelegate;

    ParsingViewController *obj;

    UIScrollView *scrollView;

}
@property (nonatomic,retain)UIScrollView *scrollView;


@end



- (void)viewDidLoad {
    [super viewDidLoad];

    int x=5,y=10;


    appDelegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate];

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];   
    scrollView.contentSize = CGSizeMake(320,460);
    scrollView.showsVerticalScrollIndicator = YES;
    scrollView.showsHorizontalScrollIndicator = YES;
    [self.view addSubview:scrollView];

    for (Litofinter *lito in appDelegate.logoArray) {

        NSString * urlString = [lito.cLogo stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
        NSURL * imageURL = [NSURL URLWithString:urlString];

        NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
        UIImage * image = [UIImage imageWithData:imageData];

        UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
        [imgView setFrame:CGRectMake(x, y, 196, 90)];
        [scrollView addSubview:imgView];


        UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapImage)];
        [imgView addGestureRecognizer:tgr];
        [tgr release];
        [imgView release];

        //Do the rest of your operations here, don't forget to release the UIImageView
        x = x + 200;

    }


}


-(void)onTapImage
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message from mAc" message:@"Trail" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
    [alert show];
}


@end

Upvotes: 1

Views: 518

Answers (1)

Totumus Maximus
Totumus Maximus

Reputation: 7583

You probably forget to set the .userInteractionEnabled property of your imageView. Make sure you set it right.

Upvotes: 1

Related Questions