Basic usage of IOS Uiscrollview

Source: Internet
Author: User

The use of Roll view is common, it is necessary to master some basic usage, here are some basic usage:

#import <UIKit/UIKit.h>

Macro definition

#define WIDTH 300

#define Height 300

#define X 60

#define Y 100

@interface viewcontroller:uiviewcontroller<uiscrollviewaccessibilitydelegate>

@property (strong,nonatomic) Uiscrollview *myscrollview;

Page

@property (strong,nonatomic) Uipagecontrol *mypagecontrol;

Storing a collection of pictures

@property (strong,nonatomic) Nsmutablearray *imagesarray;

Current page number

@property (assign,nonatomic) int currentpage;

@property (strong,nonatomic) Uiimageview *firstimage;

@property (strong,nonatomic) Uiimageview *secondimage;

@property (strong,nonatomic) Uiimageview *thirdimage;

@end

Create a roll view

Self. Myscrollview=[[uiscrollview Alloc] Initwithframe:cgrectmake (X, Y, Width,height)];

Self. Myscrollview.backgroundcolor=[uicolor Graycolor];

Self. Myscrollview.contentsize=cgsizemake (width*3,0);

Self. myscrollview.delegate=self;

Self. Myscrollview.pagingenabled=yes;

Self. Myscrollview.showshorizontalscrollindicator=no;

[Self.view addsubview:self. Myscrollview];

Initialize a collection of stored pictures

Self.imagesarray=[nsmutablearray arraywithcapacity:100];

for (int i=1; i<7; i++)

{

UIImage *images=[uiimage imagenamed:[nsstring stringwithformat:@ "%d.png", I]];

[Self.imagesarray Addobject:images];

}

Self. Firstimage=[[uiimageview alloc] init];

Self. Secondimage=[[uiimageview alloc] init];

Self. Thirdimage=[[uiimageview alloc] init];

Self. currentpage=0;

Set Page Paging

Self. Mypagecontrol=[[uipagecontrol alloc] init];

Cgsize Page=cgsizemake (120, 44);

Self. Mypagecontrol.frame=cgrectmake ((400-page.width)/2, 370, Page.width, page.height);

Self. mypagecontrol.numberofpages=6;

Self. mypagecontrol.currentpage=0;

The color of the current page number

Self. Mypagecontrol.currentpageindicatortintcolor=[uicolor Greencolor];

Specify page number color

Self. Mypagecontrol.pageindicatortintcolor=[uicolor Purplecolor];

[Self.view addsubview:self. Mypagecontrol];

Refresh

[Self reloadimage];

}

-(void) reloadimage

{

The first case shows the picture on the first page is the last picture

if (self. currentpage==0)

{

Self. Firstimage.image=[self.imagesarray Lastobject];

Self. Secondimage.image=[self.imagesarray objectatindex:self. CurrentPage];

Self. Thirdimage.image=[self.imagesarray objectatindex:self. CURRENTPAGE+1];

}

The second case shows the picture on the last page is the previous picture

else if (self. CURRENTPAGE==SELF.IMAGESARRAY.COUNT-1)

{

Self. Firstimage.image=[self.imagesarray objectatindex:self. CURRENTPAGE-1];

Self. Secondimage.image=[self.imagesarray objectatindex:self. CurrentPage];

Self. Thirdimage.image=[self.imagesarray objectatindex:0];

}

Middle page display picture starts in normal order

Else

{

Self. Firstimage.image=[self.imagesarray objectatindex:self. CURRENTPAGE-1];

Self. Secondimage.image=[self.imagesarray objectatindex:self. CurrentPage];

Self. Thirdimage.image=[self.imagesarray objectatindex:self. CURRENTPAGE+1];

}

Default is NO

Self. Firstimage.userinteractionenabled=yes;

Frame size of picture

Self. Firstimage.frame=cgrectmake (0,0,width,height);

Self. Secondimage.frame=cgrectmake (width,0, Width, Height);

Self. Thirdimage.frame=cgrectmake (width*2,0, Width, Height);

Add a picture to a scrolling view

[Self. Myscrollview addsubview:self. Firstimage];

[Self. Myscrollview addsubview:self. Secondimage];

[Self. Myscrollview addsubview:self. Thirdimage];

Offset of the scroll view

Self. Myscrollview.contentoffset=cgpointmake (Width, 0);

}

#pragma mark Delegate Agent

-(void) scrollviewdidenddecelerating: (Uiscrollview *) ScrollView

{

Float scropage=self. Myscrollview.contentoffset.x;

Swipe right

if (scropage> 2| | scropage==2)

{

if (self. CurrentPage = = (int) self.imagesarray.count-1)

{

Self. currentpage=0;

}

Else

{

Self. currentpage++;

}

}

Swipe left

if (scropage<0| | scropage==0)

{

if (self. currentpage==0)

{

Self. currentpage = (int) self.imagesarray.count-1;

}

Else

{

Self. currentpage--;

}

}

Self. Mypagecontrol.currentpage=self. CurrentPage;

Refresh

[Self reloadimage];

Basic usage of IOS Uiscrollview

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.