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
Post a Comment