IOS Uipagecontrol Simple Instance

Source: Internet
Author: User

IOS Uipagecontrol Simple Example

About automatic scrolling code to be replenished

The pictures in the instance are added by themselves

. h File Codes

#import <UIKit/UIKit.h> @interface viewcontroller:uiviewcontroller<uiscrollviewdelegate>{    Uiscrollview *_scrollview;    Nsmutablearray *slideimages;    Uipagecontrol *_page;} @end

. m file Codes

#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(ID) Initwithnibname: ( NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self = [Super Initwithnibname:nibnameornil Bundle:nibbundle    Ornil]; if (self) {//Custom initialization} return to self;}        -(void) viewdidload{[Super Viewdidload];    _scrollview = [[Uiscrollview alloc]initwithframe:cgrectmake (0, 20, 320, 240)];    _scrollview.bounces = NO;    _scrollview.pagingenabled = YES;    _scrollview.delegate = self;    _scrollview.contentoffset = Cgpointmake (320, 0);    _scrollview.contentsize = Cgsizemake (1920,240);    _scrollview.showsverticalscrollindicator =no;    _scrollview.showshorizontalscrollindicator = NO;    _scrollview.userinteractionenabled = YES;        [Self.view Addsubview:_scrollview];    Slideimages = [[Nsmutablearray alloc]initwithobjects:@ "1.png", @ "2.png", @ "3.png", @ "4.png", nil];                     Uiimageview *imageview = [[Uiimageview alloc]         Initwithimage:[uiimage imagenamed:[slideimages objectatindex: ([Slideimages count]-1)]];    Imageview.frame = CGRectMake (0, 0, 320, 240);    [_scrollview Addsubview:imageview];                                   for (int i = 0;i<[slideimages count];i++) {//loop this bit uiimageview *imageview = [[Uiimageview alloc]        Initwithimage:[uiimage imagenamed:[slideimages Objectatindex:i]];        Imageview.frame = CGRectMake (320*i+320, 0, 320, 240);        imageview.userinteractionenabled = YES;    [_scrollview Addsubview:imageview];    } ImageView = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:[slideimages objectatindex:0]];    Imageview.frame = CGRectMake (320*5, 0, 320, 240);    [_scrollview Addsubview:imageview];    _page = [[Uipagecontrol alloc]initwithframe:cgrectmake (240, 230, 70, 30)];    _page.numberofpages = 4;    _page.currentpage = 0;    _page.backgroundcolor = [Uicolor Graycolor]; [_page addtarget:self Action: @selector (Pageaction) forcontrolevents:uicontroleventtouchupinside];    [Self.view Addsubview:_page];    Do any additional setup after loading the view. }-(void) scrollviewdidenddecelerating: (Uiscrollview *) scrollview{int currentpage = (_scrollview.contentoffset.x-_SC    RollView.frame.size.width/([slideimages count]+2)/_scrollview.frame.size.width + 1;    NSLog (@ "%d", currentpage);    if (currentpage==0) {[_scrollview scrollrecttovisible:cgrectmake (320*4, 0, + +) Animated:no]; } else if (currentpage== ([slideimages count]+1)) {///If it is the last +1, that is, the first to start the loop [_scrollview Scrollrecttovisibl    E:cgrectmake (0, +) Animated:no];    }}-(void) Scrollviewdidscroll: (Uiscrollview *) sender{int page = _scrollview.contentoffset.x/320-1; _page.currentpage = page;}    -(void) pageaction{int page = _page.currentpage; [_scrollview Setcontentoffset:cgpointmake (page+1), 0)];} -(void) didreceivememorywarning{[Super didreceivememorywarning];    Dispose of any resources the can be recreated. } @end


IOS Uipagecontrol Simple Instance

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.