Header file:
# Import @ Class articleviewcontroller; @ interface articlescrollviewcontroller: uiviewcontroller {// No array is used. It looks clearer. To save memory and make it look seamless, it is best for three views: articleviewcontroller * Article1; articleviewcontroller * Article2; articleviewcontroller * Article3;} @ end
Implementation:
# Import "articlescrollviewcontroller. H "# import" articleviewcontroller. H "# import" viewswitcher. H "@ implementation articlescrollviewcontroller // The viedidload function is not important. If three views are initialized and placed in uiscrollview, set the content size of uiscrollview correctly.-(void) viewdidload {[Super viewdidload]; uiscrollview * scrollview = (uiscrollview *) self. view; scrollview. contentsize = cgsizemake (self. view. frame. size. width * 3, self. view. frame. si Ze. height); cgrect frame = self. view. frame; frame. origin. y = 0.0f; nsinteger article2index = [viewswitcher getinstance]. currentarticleindex; nsinteger article1index = article2index-1; article1index = article1index <0? [Viewswitcher getinstance]. articlecount-1: article1index; article1index = article1index> = [viewswitcher getinstance]. articlecount? 0: article1index; nsinteger article3index = article2index + 1; article3index = article3index <0? [Viewswitcher getinstance]. articlecount-1: article3index; article3index = article3index> = [viewswitcher getinstance]. articlecount? 0: article3index; Article1 = [[articleviewcontroller alloc] Comment: article1index]; [article1.view setframe: frame]; Article2 = [[articleviewcontroller alloc] initwitharticleindex: article2index]; frame. origin. X + = self. view. frame. size. width; [article2.view setframe: frame]; Article3 = [[articleviewcontroller alloc] initwitharticleindex: article3index]; frame. origin. X + = self. view. frame. size. width; [article3.view setframe: frame]; [scrollview addsubview: article1.view]; [scrollview addsubview: article2.view]; [scrollview addsubview: article3.view]; cgpoint P = cgpointzero; p. X = scrollview. frame. size. width; [scrollview setcontentoffset: P animated: No]; [Article2 reloaddata] ;}# Pragma mark-# pragma mark uiscrollviewdelegate # define set_frame (articlex) x = articlex. view. frame. origin. X + increase; If (x <0) x = pagewidth * 2; If (x> pagewidth * 2) x = 0.0f; [articlex. view setframe: cgrectmake (x, articlex. view. frame. origin. y, articlex. view. frame. size. width, articlex. view. frame. size. height)] // move all three views to the right, and update the three pointers. Article 2 always points to the currently displayed view, and Article 1 is on the left, article3 is the right-(void) Comment :( cgfloat) pagewidth {// previous article article3.articleindex = article1.articleindex-1; if (article3.articleindex <0) {article3.articleindex = [viewswitcher getinstance]. articlecount-1;} [Article1 reloaddata]; articleviewcontroller * Comment = Article3; Article3 = Article2; Article2 = Article1; Article1 = comment; float increase = pagewidth; cgfloat x = 0.0f; set_frame (Article3); set_frame (article1); set_frame (Article2);}-(void) allarticlesmoveleft :( cgfloat) pagewidth {article1.articleindex = article3.articleindex + 1; if (article1.articleindex> = [viewswitcher getinstance]. articlecount) {comment = 0;} [Article3 reloaddata]; // [Article2 resetview]; [Article3 resetview]; articleviewcontroller * tmparticleviewcontroller = Article1; Article1 = Article2; Article2 = Article3; article3 = tmparticleviewcontroller; float increase =-pagewidth; cgfloat x = 0.0f; set_frame (Article2); set_frame (Article3); set_frame (article1 );} /* loop scrolling: after each scroll, the offset of scrollview is set to the middle page. If this scroll is a scroll of the forward page, all three pages are placed backward, the last page is placed at the beginning. If this scroll is backward, all three pages are placed forward. The first page is placed at the end */-(void) scrollviewdidenddecelerating :( uiscrollview *) thescrollview {cgfloat pagewidth = thescrollview. frame. size. width; // 0 1 2 int page = floor (thescrollview. contentoffset. x-pagewidth/2)/pagewidth) + 1; if (page = 1) {// The user dragged, but the rolling event does not take effect return ;} else if (page = 0) {[self allarticlesmoveright: pagewidth];} else {[self allarticlesmoveleft: pagewidth];} cgpoint P = cgpointzero; p. X = pagewidth; [thescrollview setcontentoffset: P animated: No]; [Article1 resetview]; [Article3 resetview];} # pragma mark-# pragma mark dealloc-(void) didreceivememorywarning {// releases the view if it doesn' t have a superview. [Super didreceivememorywarning]; // release any cached data, images, etc. that aren't in use .} -(void) viewdidunload {[Super viewdidunload]; // release any retained subviews of the main view. // e.g. self. myoutlet = nil; [Article1 release]; Article1 = nil; [Article2 release]; Article2 = nil; [Article3 release]; Article3 = nil;}-(void) dealloc {[Article1 release]; [Article2 release]; [Article3 release]; [Super dealloc] ;}@ end
Here, the minor skill is:
Drag your finger to the right as an example. The screen refers to the display area of scorllview.
Article1 Article2 Article3 [screen]
Drag the image as follows:
Article1 Article2 Article3 [screen] place Article3 In the first frame (set the frame of Article3). This screen also displays the content of article 1.
Article3 Article1 Article2 [screen]
Move the screen to the center: Use setcontentoffset to disable the animation, so as to trick the human eye
Article3 Article1 Article2 [screen]
Last update pointer sequence:
Article1 Article2 Article3 [screen]
Seamless loops are implemented.
Loop playback View