Ios-scrollview Create Android-viewpager Effect

Source: Internet
Author: User

1. Put the picture into the Images folder and rename it to Images.bundle. Drag into the project



2 drag ScrollView and press scroll view in the upper-left corner to select Deleagte in the View controller.

This time. Specifies that the proxy for scroll view is controller.



Add a proxy to the specified controller

#import <UIKit/UIKit.h> @interface Cszviewcontroller:uiviewcontroller <uiscrollviewdelegate>// Viewpager@property (Weak, nonatomic) Iboutlet Uiscrollview *scrollview; @end

@implementation cszviewcontroller-(void) viewdidload{[Super Viewdidload];    int width=self.view.frame.size.width;        int height=self.view.frame.size.height;    Remove multiple pages of content path from bundle compressed file NSString *bundlepath=[[nsbundle Mainbundle] pathforresource:@ "Images" oftype:@ "bundle"];    NSBundle *bundle=[[nsbundle alloc] Initwithpath:bundlepath]; Self.imagesarr=[bundle pathsforresourcesoftype:@ "JPG" indirectory:nil];self.scrollview.contentsize=cgsizemake (        Self.imagesarr.count*width,height);        Create paging for (int i=0; i<self.imagesarr.count; i++) {uiimageview *itemimage=[[uiimageview alloc] init];        Itemimage.image=[uiimage Imagewithcontentsoffile:self.imagesarr[i]];        Itemimage.frame=cgrectmake (i*width, 0, width, height);    [Self.scrollview Addsubview:itemimage];    }//Remove the slider self.scrollview.showshorizontalscrollindicator=no that appears when sliding;    Make Viewpager elastic. Split each page self.scrollview.pagingenabled=yes; Create an identity Uipagecontrol *ctr=[[uipagecontrolALLOC] init];    Ctr.bounds=cgrectmake (0, 0, 150, 50);    Ctr.center=cgpointmake (width*0.5, height-50);    Ctr.currentpageindicatortintcolor=[uicolor Redcolor];    Ctr.pageindicatortintcolor=[uicolor Graycolor];    ctr.currentpage=1; The number of declaration indicators.    If not set will not show Ctr.numberofpages=self.imagesarr.count;    [Ctr addtarget:self Action: @selector (pageindict:) forcontrolevents:uicontroleventvaluechanged];    [Self.view addsubview:ctr]; Because Self.view is already bound once, Pagecontrol is a weak reference self.pagecontrol=ctr;}    #pragma mark Click Indicator Jump viewpager-(ibaction) Pageindict: (ID) sender{int curr=self.pagecontrol.currentpage;    NSLog (@ "%d", Curr); Self.scrollview.contentoffset=cgpointmake (Self.view.frame.size.width*curr, 0);} #pragma mark stops when sliding Viewpager to allow the corresponding indicator to change-(void) scrollviewdidenddecelerating: (Uiscrollview *) scrollview{int OffSetX    =self.scrollview.contentoffset.x;    int index=offsetx/self.view.frame.size.width;    Self.pagecontrol.currentpage=index; } @end


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.