ios - UIScrollView not scrolling when adding UIButtons as subviews -
i'm trying build simple uiscrollview paging horizontally scroll between 3 images. tricky part each image clickable , catch click event.
my technique create 3 uibuttons each consists uiimage. give each button tag , set action.
problem: can catch click event - but it's not scrollable!
here code:
- (void) viewdidappear:(bool)animated { _imagearray = [[nsarray alloc] initwithobjects:@"content_01.png", @"content_02.png", @"content_03.png", nil]; (int = 0; < [_imagearray count]; i++) { //we'll create imageview object in every 'page' of our scrollview. cgrect frame; frame.origin.x = _contentscrollview.frame.size.width * i; frame.origin.y = 0; frame.size = _contentscrollview.frame.size; // //get image use, want uiimage* image = [uiimage imagenamed:[_imagearray objectatindex:i]]; uibutton* button = [[uibutton alloc] initwithframe:frame]; //set button states want image show [button setimage:image forstate:uicontrolstatenormal]; [button setimage:image forstate:uicontrolstatehighlighted]; //create touch event target, calling 'productimagepressed' method [button addtarget:self action:@selector(imagepressed:) forcontrolevents:uicontroleventtouchupinside]; //i set tag image #, looking though array of them button.tag = i; [_contentscrollview addsubview:button]; } //set content size of our scrollview according total width of our imageview objects. _contentscrollview.contentsize = cgsizemake(_contentscrollview.frame.size.width * [_imagearray count], _contentscrollview.frame.size.height); _contentscrollview.backgroundcolor = [engappdelegate backgroundcolor]; _contentscrollview.delegate = self; }
well, since uibutton
uicontrol
subclass, "eats up" touches of scroll view:
[uiscrollview touchesshouldcancelincontentview:]
default returned value yes if view not uicontrol object; otherwise, returns no.
you could influence subclassing uiscrollview
, overwriting touchesshouldcancelincontentview:
(and/or touchesshouldbegin:withevent:incontentview:
). however, use case i'd not use buttons in first place. why not add tap gesture recognizer scrollview , use touch point determine image has been tapped? that's easier , should work without issues.
Comments
Post a Comment