Encapsulating paging functionality

Source: Internet
Author: User

#import <UIKit/UIKit.h>

@interface Lzjpageview:uiview

/** Picture name Data * /

@property (nonatomic, strong) Nsarray *imagenames;

+ (instancetype) PageView;

@end

#import "LZJPageView.h"

@interface Lzjpageview () <UIScrollViewDelegate>

@property (weak, nonatomic) iboutlet uiscrollview *scrollview;

@property (weak, nonatomic) iboutlet uipagecontrol *pagecontrol;

/** Timer * /

@property (nonatomic, weak) Nstimer *timer;

@end

@implementation Lzjpageview

-(void) awakefromnib

{

//1. whether to hide Pagecontrol When a single page

self. pagecontrol.hidesforsinglepage = YES;

//2. set up a picture of Pagecontrol

[self. Pagecontrol setvalue:[uiimage imagenamed:@ ' current '] Forkeypath:@ "_currentpageimage" ];

[self. Pagecontrol setvalue:[uiimage imagenamed:@ "Other"] Forkeypath: @ "_pageimage"];

//3. Turn on timer

[self starttimer];

}

+ (instancetype) PageView

{

return [[[NSBundle Mainbundle] Loadnibnamed:nsstringfromclass (self) owner:Nil options: Nil] lastobject];

}

-(void) Setimagenames: (Nsarray *) imagenames

{

_imagenames = Imagenames;

//0. removing the previously added

// let subviews every object in this array is executed removefromsuperview

[self. Scrollview.subviews makeobjectsperformselector:@selector(Removefromsuperview)];

//1. Create ImageView based on picture name Data to add to scrollView

CGFloat scrollvieww = self. scrollView.frame.size.width;

CGFloat SCROLLVIEWH = self. scrollView.frame.size.height;

Nsuinteger count = Imagenames.count;

for (int i = 0; i < count; i + +) {

Uiimageview *imageview = [[Uiimageview alloc] init];

Imageview.image = [UIImage imagenamed:imagenames[i]];

Imageview.frame = CGRectMake (i * scrollvieww, 0, SCROLLVIEWW, SCROLLVIEWH);

[self. ScrollView Addsubview:imageview];

}

//2. set contentsize

self. scrollview.contentsize = Cgsizemake (Count * scrollvieww, 0);

//3. Set Total pages

self. pagecontrol.numberofpages = count;

}

#pragma mark- timer-related code

-(void) Starttimer

{

// Returns an automatically executed timer object

self. timer = [Nstimer scheduledtimerwithtimeinterval:2.0 target:self selector:@selector (nextPage:) UserInfo:@ "123" repeats:YES];

[[Nsrunloop Mainrunloop] AddTimer:self. Timer formode:nsrunloopcommonmodes];

}

-(void) Stoptimer

{

[self. Timer invalidate];

}

/**

* Scroll to the next page

*/

-(void) NextPage: (Nstimer *) Timer

{

//1. Calculate the page number of the next page

Nsinteger page = self. Pagecontrol.currentpage + 1;

//2. more than the last page

if (page = = self . Imagenames.count) {

page = 0;

}

//3. Scroll to the next page

[self. ScrollView setcontentoffset:cgpointmake (page * self. ScrollView.frame.size.width, 0) Animated:YES];

}

#pragma mark-uiscrollviewdelegate

-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView

{

//1. calculate page Numbers

int page = (int) (scrollview.contentoffset.x/scrollview.frame.size.width + 0.5);

//2. set page numbers

self. pagecontrol.currentpage = page;

}

/**

* Stop timer when user is about to start dragging ScrollView

*/

-(void) scrollviewwillbegindragging: (Uiscrollview *) ScrollView

{

[self stoptimer];

}

/**

* When the user has stopped dragging the ScrollView, turn on the timer

*/

-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate

{

[self starttimer];

}

@end

Encapsulating paging functionality

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.