iOS with Uipagecontroller and Uiscrollview

Source: Internet
Author: User

Uipagecontroller

Uipagecontroller controls are primarily used to implement view paging and are typically used in conjunction with Uiscrollview

Create

Uipagecontrol *page=[[uipagecontrol alloc] init];

Set frame

Pagel.frame = CGRectMake (0,self.view.frame.size.height-40,self.view.frame.size.width,20);

Common Properties

Number of sub-pages

Page.numberofpages = Self.imageNames.count;

Set page turn mark render color

Page.pageindicatortintcolor = [Uicolor blackcolor];

Set the rendering color of the current page

Pagel.currentpageindicatortintcolor =[uicolor Redcolor];

Turn off user interaction

page.userinteractionenabled = NO;

Add to Parent View

[Self.view Addsubview:page];

Uipagecontroller used in conjunction with the Uiscrollview

The first thing to do is to obey the <UIScrollViewDelegate> protocol, and then assign a value to delegate in the Viewdidload: method,

Finally implement the Protocol in the Scrollviewdidscroll: Method, the method will be called every time the scroll is completed, that is, in the method to calculate the current display of the page and assigned to Self.pageControl.currentPage

-(void) viewdidload

{

[Super Viewdidload];

Uiscrollview *scrollview=[[uiscrollview alloc] init];

CGRect Frame=self.view.frame;

Scrollview.frame=frame;

UIImage *image1=[uiimage imagenamed:@ "1.jpg"];

UIImage *image2=[uiimage imagenamed:@ "2.jpg"];

UIImage *image3=[uiimage imagenamed:@ "3.jpg"];

[email protected] [Image1,image2,image3];

Cgsize Size=cgsizemake (Self.array.count*self.view.frame.size.width, self.view.frame.size.height);

Show content Size

Scrollview.contentsize=size;

for (int i=0;i<self.array.count;i++)

{

Uiimageview *imageview=[[uiimageview alloc] initwithimage:self.array[i];

Picture Display Form

Imageview.contentmode=uiviewcontentmodescaleaspectfit;

Picture Display Range

Imageview.frame=cgrectmake (i*self.view.frame.size.width, 0, Self.view.frame.size.width, Self.view.frame.size.height);

Add to ScrollView.

[ScrollView Addsubview:imageview];

}

Full page flipping

Scrollview.pagingenabled=yes;

Uipagecontrol *page=[[uipagecontrol alloc] init];

Self.page=page;

Position

Page.frame = CGRectMake (0,self.view.frame.size.height-40, self.view.frame.size.width,20);

Number of pages

Page.numberofpages=self.array.count;

Set color

Page.currentpageindicatortintcolor=[uicolor Redcolor];

Page.pageindicatortintcolor=[uicolor Blackcolor];

Set yourself as a. Delegate

scrollview.delegate=self;

Add to view

[Self.view Addsubview:scrollview];

[Self.view Addsubview:page];

}

Implementation method

Finally implement the page's indicator with ScrollView scrolling display function, this function needs to be implemented by ScrollView delegate.

-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView

{

Get the coordinates of the moving relative origin of the picture.

Cgpoint Point=scrollview.contentoffset

The movement cannot exceed the left;

if (point.x<0)

{

point.x=0;

Scrollview.contentoffset=point;

}

Move cannot exceed right

if (point.x> (self.array.count-1) *self.view.frame.size.width)

{

point.x=self.view.frame.size.width* (self.array.count-1);

Scrollview.contentoffset=point;

}

Judging pages based on picture coordinates

Nsinteger Index=round (point.x/self.view.frame.size.width);

Self.page.currentpage=index;

}

Disclaimer: Reference Dahne Notes

iOS with Uipagecontroller and 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.