objective c - Touch method UIImageView inside UIScrollView -


i wondering how can use touch method uiimageview inside uiscrollview in xcode. when add uiimageview subview self.view, can use touch method. when add uiimageview subview uiscrollview, can't. how can solve this?

this code:

- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event {     touches = [event alltouches];     (uitouch *touch in touches) {          nslog(@"image touched");     } }  - (void)viewdidload {     [super viewdidload];      uiscrollview *scrollview = [[uiscrollview alloc] initwithframe:cgrectmake(0, 44, [uiscreen mainscreen].bounds.size.width, [uiscreen mainscreen].bounds.size.height * 0.9)];     scrollview.scrollenabled = true;     scrollview.bounces = true;     scrollview.contentsize = cgsizemake([uiscreen mainscreen].bounds.size.width, [uiscreen mainscreen].bounds.size.height);     scrollview.userinteractionenabled = yes;     [self.view addsubview:scrollview];       uiimageview *imageview = [uiimageview alloc] initwithframe:cgrectmake([uiscreen mainscreen].bounds.size.width * 0.04, 10, [uiscreen mainscreen].bounds.size.width * 0.28, [uiscreen mainscreen].bounds.size.height * 0.22)];      imageview.image = [uiimage imagenamed(@"image.png")];      imageview.layer.bordercolor = [uicolor whitecolor].cgcolor;      imageview.layer.borderwidth = 1;      imageview.userinteractionenabled = yes;      [scrollview addsubview:imageview]; } 

give uigesturerecognizers try. far easier manage multiple layers of touch management.

- (void)touchedimage:(uitapgesturerecognizer *)gesture {     // when gesture has ended, perform action.     if (gesture.state == uigesturerecognizerstateended) {         nslog(@"touched image");     } }  - (void)viewdidload {     [super viewdidload];      uiscrollview *scrollview = [[uiscrollview alloc] initwithframe:cgrectmake(0, 44, [uiscreen mainscreen].bounds.size.width, [uiscreen mainscreen].bounds.size.height * 0.9)];     scrollview.scrollenabled = true;     scrollview.bounces = true;     scrollview.contentsize = cgsizemake([uiscreen mainscreen].bounds.size.width, [uiscreen mainscreen].bounds.size.height);     scrollview.userinteractionenabled = yes;     [self.view addsubview:scrollview];      uiimageview *imageview = [[uiimageview alloc] initwithframe:cgrectmake([uiscreen mainscreen].bounds.size.width * 0.04, 10, [uiscreen mainscreen].bounds.size.width * 0.28, [uiscreen mainscreen].bounds.size.height * 0.22)];     imageview.image = [uiimage imagenamed:@"image.png"];     imageview.layer.bordercolor = [uicolor whitecolor].cgcolor;     imageview.layer.borderwidth = 1;     imageview.userinteractionenabled = yes;     [scrollview addsubview:imageview];      // create tap gesture     uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(touchedimage:)];     [imageview addgesturerecognizer:tap]; } 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -