Saturday 15 August 2015

ios - How to create slideshow of images within scrollview with autolayout? -



ios - How to create slideshow of images within scrollview with autolayout? -

i have collectionview loads series of cells. of cells have static images (which works well), might have multiple images need swipeable.

the uicollectionviewcell base of operations view, uiscrollview within (pinned top, left, bottom, , right of superview).

adding uiimageview scrollview, want pin top , left of scrollview, have width , height same collection view cell, not content size of scrollview.

then i'd 2 additional images pinned top of collection view cell, , left closest view (in case, prior uiimageview).

the storyboard this:

(with other images off-screen , not displayed).

i decided add together images via code:

there's parent view, , scrollview (attached iboutlet).

the collectionviewcell subclass contains:

@synthesize lbl, sv, pagecontrol; - (void) awakefromnib { sv.userinteractionenabled = no; sv.delegate = self; pagecontrol.userinteractionenabled = no; [self.contentview addgesturerecognizer:sv.pangesturerecognizer]; } - (void)scrollviewdidscroll:(uiscrollview *)scrollview { cgfloat pagewidth = sv.frame.size.width; float fractionalpage = sv.contentoffset.x / pagewidth; nsinteger page = lround(fractionalpage); self.pagecontrol.currentpage = page; }

then, within view controller's collectionview:cellforitematindexpath method, manually add together images so:

uiimageview *img = [[uiimageview alloc] initwithimage:[uiimage imagenamed:[myarray objectatindex:indexpath.row]]]; img.frame = (cgrect) { 0, 0, cell.frame.size.width, cell.frame.size.height}; [cell.sv addsubview:img]; img = [[uiimageview alloc] initwithimage:[uiimage imagenamed:[myarray objectatindex:indexpath.row + 1]]]; img.frame = (cgrect) { cell.frame.size.width, 0, cell.frame.size.width, cell.frame.size.height }; [cell.sv addsubview:img]; img = [[uiimageview alloc] initwithimage:[uiimage imagenamed:[myarray objectatindex:indexpath.row + 2]]]; img.frame = (cgrect) { cell.frame.size.width * 2, 0, cell.frame.size.width, cell.frame.size.height }; [cell.sv addsubview:img]; [cell.sv setcontentsize:cgsizemake(cell.frame.size.width * 3.0, cell.frame.size.height)];

i have preferred storyboard/interface builder way of doing this, now, suffice.

ios xcode uiscrollview autolayout uicollectionviewcell

No comments:

Post a Comment