iOS Carousel diagram

Source: Internet
Author: User

The main techniques for carousel playback are:

The encapsulation of the cell. This is achieved using Uicollectionviewcell.

#import <UIKit/UIKit.h>

@interface Circleviewcell:uicollectionviewcell

@property (nonatomic, strong) uiimage* image;

@property (nonatomic, assign) Nsinteger index;

@end

//

circleviewcell.m

Lunbo

//

Created by Jiaoxiangjie on 16/10/12.

COPYRIGHT©2016 year lumen. All rights reserved.

//

#import "CircleViewCell.h"

@interface Circleviewcell ()

@property (nonatomic, strong) uiimageview* ImageView;

@end

@implementation Circleviewcell

-(Instancetype) init{

if (self = [super init]) {

[Self myinit];

}

return self;

}

-(Instancetype) Initwithcoder: (Nscoder *) Adecoder

{

if (self = [super Initwithcoder:adecoder])

[Self myinit];

return self;

}

-(Instancetype) initWithFrame: (CGRect) frame

{

if (self = [super Initwithframe:frame])

[Self myinit];

return self;

}

-(void) layoutsubviews

{

[Super Layoutsubviews];

Self.imageview.frame = Self.contentView.bounds;

}

-(uiimageview*) ImageView

{

if (_imageview = = nil) {

_imageview = [[Uiimageview alloc] init];

_imageview.contentmode = Uiviewcontentmodescaleaspectfill;

_imageview.userinteractionenabled = YES;

_imageview.backgroundcolor = [Uicolor Whitecolor];

}

return _imageview;

}

-(void) SetImage: (UIImage *) image

{

_image = image;

Self.imageview.image = image;

}

//

CircleScrollView.h

Lunbo

//

Created by Jiaoxiangjie on 16/10/12.

COPYRIGHT©2016 year lumen. All rights reserved.

//

#import <UIKit/UIKit.h>

@class Circlescrollview;

@protocol circlescrollviewdelegate <NSObject>

-(void) Didclickimageatindex: (nsinteger) Index ScrollView: (Circlescrollview *) ScrollView;

@end

@interface Circlescrollview:uiview

@property (nonatomic, weak) id<circlescrollviewdelegate> delegate;//set up proxy

@property (nonatomic, assign) Nstimeinterval duringtimel;//time interval

-(void) Images: (nsarray*) images;

-(void) Closetimer;

-(void) Opentimer;

@end

-(void) myinit{

[Self.contentview AddSubview:self.imageview];

}

@end

//

circlescrollview.m

Lunbo

//

Created by Jiaoxiangjie on 16/10/12.

COPYRIGHT©2016 year lumen. All rights reserved.

//

#import "CircleScrollView.h"

#import "CircleViewCell.h"

@interface Circlescrollview () <UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) uicollectionview* CollectionView;

@property (nonatomic, strong) nsarray* images;//image Array

@property (nonatomic, assign) Nsinteger imagecount;//pictures

@property (nonatomic, strong) nsmutablearray* celldata;//array

@property (nonatomic, strong) uipagecontrol* Pagecontrol;

@property (nonatomic, strong) nstimer* timer;

@property (nonatomic, strong) nslock* mlock;//plus lock for multithreading

@end

@implementation Circlescrollview

static NSString *collectioncellid = @ "Collectioncellid";

-(Instancetype) initWithFrame: (CGRect) frame

{

if (self = [Super Initwithframe:frame]) {

Initializing data information

uicollectionviewflowlayout* FlowLayout = [[Uicollectionviewflowlayout alloc] init];

Flowlayout.itemsize = frame.size;

Flowlayout.scrolldirection = Uicollectionviewscrolldirectionhorizontal;

flowlayout.minimumlinespacing = 0;

flowlayout.minimuminteritemspacing = 0;

Flowlayout.sectioninset = uiedgeinsetsmake (0, 0, 0, 0);

Self.collectionview = [[Uicollectionview alloc] InitWithFrame:self.bounds

Collectionviewlayout:flowlayout];

Self.collectionView.delegate = self;

Self.collectionView.dataSource = self;

self.collectionView.pagingEnabled = YES;

Self.collectionView.showsHorizontalScrollIndicator = NO;

Self.collectionView.showsVerticalScrollIndicator = NO;

Self.collectionView.alwaysBounceHorizontal = YES;

self.collectionView.alwaysBounceVertical = NO;

Self.collectionView.backgroundColor = [Uicolor Whitecolor];

[Self.collectionview Registerclass:[circleviewcell class]

Forcellwithreuseidentifier:collectioncellid];

[Self addSubview:self.collectionView];

Self.pagecontrol = [[Uipagecontrol alloc] Initwithframe:cgrectmake (0, self.bounds.size.height-30, Self.bounds.size.width, 30)];

Self.pageControl.tag = 100;

self.pageControl.userInteractionEnabled = NO;

[Self AddSubview:self.pageControl];

[Self BringSubviewToFront:self.pageControl];

Self.backgroundcolor = [Uicolor Whitecolor];

Self.mlock = [[Nslock alloc] init];

}

return self;

}

-(void) Setduringtimel: (nstimeinterval) Duringtimel

{

_duringtimel = Duringtimel;

if (Duringtimel < 0.001)

Return

[Self closetimer];

[Self opentimer];

}

-(void) Images: (nsarray*) images

{

[Self.mlock Lock];

[Self closetimer];

_images = images;

_imagecount = Images.count;

Self.celldata = [[Nsmutablearray alloc] init];

for (Nsinteger i = 0; i <; i++) {

for (Nsinteger j = 0; J < _imagecount; J + +) {

[Self.celldata addobject:@ (j)];

}

}

[Self.collectionview Reloaddata];

[Self.collectionview scrolltoitematindexpath:[nsindexpath indexpathforrow:50 * _imagecount inSection:0]

Atscrollposition:uicollectionviewscrollpositionleft

Animated:no];

Set up

Self.pageControl.hidden = _imagecount > 0? No:yes;

Self.pageControl.numberOfPages = _imagecount;

self.pageControl.currentPage = 0;

[Self opentimer];

[Self.mlock unlock];

}

-(void) Closetimer

{

if (Self.timer)

[Self.timer invalidate];

}

-(void) Opentimer

{

if (_duringtimel > 0.8)

Self.timer = [Nstimer Scheduledtimerwithtimeinterval:_duringtimel

Target:self

Selector: @selector (OnTimer)

Userinfo:nil

Repeats:yes];

}

-(void) OnTimer

{

if (Self.cellData.count > 0) {

nsarray* array = [Self.collectionview indexpathsforvisibleitems];

if (Array.count = = 0)

Return

nsindexpath* Indexpath = array[0];

Nsinteger row = Indexpath.row;

if (row% _imagecount = = 0) {

row = * _imagecount;

[Self.collectionview scrolltoitematindexpath:[nsindexpath Indexpathforrow:row insection:0] AtScrollPosition: Uicollectionviewscrollpositionleft Animated:no];

}

[Self.collectionview scrolltoitematindexpath:[nsindexpath Indexpathforrow:row + 1 insection:0] AtScrollPosition: Uicollectionviewscrollpositionleft Animated:yes];

Self.pageControl.currentPage = (row + 1)% _imagecount;

}

}

-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView {

return 1;

}

-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section {

return self.cellData.count;

}

-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath

{

circleviewcell* cell = [CollectionView dequeuereusablecellwithreuseidentifier:collectioncellid forIndexPath: Indexpath];

Nsinteger index = [_celldata[indexpath.row] integervalue];

Cell.image = _images[index];

Cell.index = index;

return cell;

}

-(void) scrollviewdidenddecelerating: (Uiscrollview *) ScrollView {

Animation stops, repositioning to the 50th set of models

int inc = ((int) (scrollview.contentoffset.x/scrollview.frame.size.width))% _imagecount;

[Self.collectionview scrolltoitematindexpath:[nsindexpath indexpathforrow:50 * _imagecount + inc inSection:0] Atscrollposition:uicollectionviewscrollpositionleft Animated:no];

Set Pagecontrol

self.pageControl.currentPage = Inc;

}

-(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) IndexPath {

Circleviewcell *cell = (Circleviewcell *) [CollectionView Cellforitematindexpath:indexpath];

if ([Self.delegate respondstoselector: @selector (Didclickimageatindex:scrollview:)]) {

[Self.delegate DidClickImageAtIndex:cell.index scrollview:self];

}

}

@end

//

Viewcontroller.m

Lunbo

//

Created by Jiaoxiangjie on 16/10/12.

COPYRIGHT©2016 year lumen. All rights reserved.

//

#import "ViewController.h"

#import "CircleScrollView.h"

@interface Viewcontroller () <CircleScrollViewDelegate>

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Self.view.backgroundColor = [Uicolor Lightgraycolor];

Cgsize size = [UIScreen mainscreen].bounds.size;

circlescrollview* ScrollView = [[Circlescrollview alloc] Initwithframe:cgrectmake (0, $, size.width, Size.width * 504/10 80)];

Scrollview.delegate = self;

[ScrollView images:@[[uiimage imagenamed:@ "photo_loading"]]; Occupy bitmap

[Self.view Addsubview:scrollview];

UIImage *m1 = [UIImage imagenamed:@ "m1.jpg"];

UIImage *m2 = [UIImage imagenamed:@ "m2.jpg"];

UIImage *m3 = [UIImage imagenamed:@ "m3.jpg"];

UIImage *m4 = [UIImage imagenamed:@ "m4.jpg"];

UIImage *M5 = [UIImage imagenamed:@ "m5.jpg"];

circlescrollview* scroll1 = [[Circlescrollview alloc] Initwithframe:cgrectmake (0, up, size.width, Size.width * 504/1080 )];

Scroll1.delegate = self;

Scroll1.duringtimel = 1.0;

[Scroll1 images:@[m1,m2]];

circlescrollview* view = [[Circlescrollview alloc] Initwithframe:cgrectmake (0, 420, size.width, Size.width * 504/1080)] ;

View.delegate = self;

[View images:@[m1, M2, M3, M4, M5]];

[Self.view Addsubview:view];

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

}

-(void) Didclickimageatindex: (nsinteger) Index ScrollView: (Circlescrollview *) ScrollView {

NSLog (@ "%li", index);

}

@end

:

iOS Carousel diagram

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.