IOS: Infinite loop Carousel Diagram Simple Package

Source: Internet
Author: User

Carousel Diagram Ideas:

1. First, there is a scrollview and two pictures, put the picture on the ScrollView.

2. To scroll the width of the scrollview you need to set the screen width of twice times.

3. Circular scrolling, when scrolling to the last picture, using the offset method, the offset to zero, back to the first picture.

4. Assign all values on the second picture to the first picture.

5. Set it to scroll itself and add a timer.

Third-party databases required: Sdwebimage

M. in the file:

#imporst "ScrollView.h"

@interface ScrollView () <UIScrollViewDelegate>

Scrolling view

@property (Nonatomic,strong) Uiscrollview *scrollview;

Image

@property (Nonatomic,strong) Uiimageview *leftimageview;

Image

@property (Nonatomic,strong) Uiimageview *rightimageview;

Indicates small dots

@property (Nonatomic,strong) Uipagecontrol *pagecontrol;

Subscript

@property (nonatomic,assign) Nsinteger index;

Timer

@property (Nonatomic,strong) Nstimer *timer;

Receive values that come in from outside

@property (Nonatomic,strong) Nsarray *dataarray;

@end

@implementation ScrollView

Lazy Loading of scrolling view

-(Uiscrollview *) scrollview{

if (!_scrollview) {

Initializing a scrolling view

_scrollview = [[Uiscrollview alloc]initwithframe:cgrectmake (0, 0, Self.frame.size.width, self.frame.size.height)];

Set up Proxy

_scrollview.delegate = self;

Cancel Bounce Effect

_scrollview.bounces = NO;

_scrollview.bounceszoom = NO;

Set full page Tumbling

_scrollview.pagingenabled = YES;

Scrolling Range

_scrollview.contentsize = Cgsizemake (self.frame.size.width*2, 0);

The closing of the scroll bar

_scrollview.showshorizontalscrollindicator = NO;

}

return _scrollview;

}

Left picture lazy Loading

-(Uiimageview *) leftimageview{

if (!_leftimageview) {

Initialize picture frame

_leftimageview = [[Uiimageview alloc]initwithframe:cgrectmake (0, 0, Self.frame.size.width, self.frame.size.height)];

Add a picture (write this step to introduce a third-party database)

[_leftimageview Sd_setimagewithurl:[nsurl urlwithstring:self.dataarray[0]];

}

return _leftimageview;

}

Right picture lazy loading

-(Uiimageview *) rightimageview{

if (!_rightimageview) {

Initialize picture frame

_rightimageview = [[Uiimageview alloc]initwithframe:cgrectmake (self.leftImageView.frame.size.width, 0, Self.frame.size.width, Self.frame.size.height)];

Add a picture (write this step to introduce a third-party database)

[_rightimageview Sd_setimagewithurl:[nsurl Urlwithstring:self.dataarray[self.index]];

}

return _rightimageview;

}

Small dot indicator lazy loading

-(Uipagecontrol *) pagecontrol{

if (!_pagecontrol) {

Initialize Small dots

_pagecontrol = [[Uipagecontrol alloc]initwithframe:cgrectmake (SELF.SCROLLVIEW.FRAME.SIZE.HEIGHT-50, SELF.FRAME.SIZE.WIDTH-100, 50)];

Set Number of pages

_pagecontrol.numberofpages = Self.dataArray.count;

Sets the dot color of the dot indicator

_pagecontrol.pageindicatortintcolor = [Uicolor Redcolor];

Sets the color of the current small dot

_pagecontrol.currentpageindicatortintcolor = [Uicolor Greencolor];

}

return _pagecontrol;

}

Timer Lazy Loading

-(Nstimer *) timer{

if (!_timer) {

Timing Time and events

_timer = [Nstimer scheduledtimerwithtimeinterval:2 target:self selector: @selector (timeraction) Userinfo:nil repeats: YES];

}

return _timer;

}

Overriding the parent class

-(Instancetype) initWithFrame: (CGRect) frame DataArray: (Nsarray *) dataarray{

if ([Super Initwithframe:frame]) {

First picture

Self.index = 1;

The value of the outside is introduced

Self.dataarray = DataArray;

[Self creatscrollviewwithframe:frame];

}

return self;

}

Create a scrolling view

-(void) Creatscrollviewwithframe: (CGRect) Frame {

[Self.scrollview AddSubview:self.leftImageView];

[Self.scrollview AddSubview:self.rightImageView];

[Self addSubview:self.scrollView];

[Self AddSubview:self.pageControl];

Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2 * nsec_per_sec)), Dispatch_get_main_queue (), ^{

[Self.timer fire];

});

}

Proxy method

-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{

If the offset of the scrolling view is screen width

if (scrollview.contentoffset.x = = self.frame.size.width) {

Returns 0

Self.scrollView.contentOffset = Cgpointzero;

The current small dot is subscript

Self.pageControl.currentPage = Self.index;

Subscript Plus +

self.index++;

The array's subscript is set to index

if (Self.index = = Self.dataArray.count) {

Starting from 0

Self.index = 0;

}

Transfer values between pictures

Self.leftImageView.image = Self.rightImageView.image;

[Self.rightimageview Sd_setimagewithurl:[nsurl Urlwithstring:self.dataarray[self.index]];

}

}

Timer events

-(void) timeraction{

[Self.scrollview setcontentoffset:cgpointmake (self.frame.size.width, 0) Animated:yes];

}

H. In the document:

#import <UIKit/UIKit.h>

@interface Scrollview:uiview

-(Instancetype) initWithFrame: (CGRect) frame DataArray: (Nsarray *) DataArray;

@end

IOS: Infinite loop carousel diagram Simple encapsulation

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.