IOS Carousel Diagram Implementation

Source: Internet
Author: User


#define Screen_size [UIScreen mainscreen].bounds.size
#define KIMAGECOUNT 3
#define KIMAGE_HEIGHT 250

@interface Viewcontroller () <UIScrollViewDelegate>
@property (nonatomic, Strong) Uiscrollview * ScrollView;
@property (nonatomic, Strong) Uipagecontrol * Pagecontrol;
@property (nonatomic, strong) Nstimer * timer;
@end

@implementation Viewcontroller
-(Uiscrollview *) scrollview{
if (_scrollview = = nil) {
_scrollview = [[Uiscrollview alloc] Initwithframe:cgrectmake (0, 0, Screen_size.width, kimage_height)];
_scrollview.pagingenabled = YES;
_scrollview.showshorizontalscrollindicator = NO;
}
return _scrollview;
}
-(Uipagecontrol *) pagecontrol{
if (_pagecontrol = = nil) {
_pagecontrol = [[Uipagecontrol alloc] Initwithframe:cgrectmake (0, self.scrollview.frame.size.height-30, SCREEN_ Size.width, 20)];
_pagecontrol.currentpageindicatortintcolor = [Uicolor colorwithred:65/255.0 green:168/255.0 blue:100/255.0 alpha:1.0 ];
_pagecontrol.pageindicatortintcolor = [Uicolor Graycolor];

}
return _pagecontrol;
}
-(void) setview{
[Self.view AddSubview:self.scrollView];
[Self.view AddSubview:self.pageControl];
CGFloat imagey = 0;
CGFloat Imagew = screen_size.width;
CGFloat Imageh = kimage_height;
for (int i = 0; i < Kimagecount; i++) {
Uiimageview * ImageView = [[Uiimageview alloc] Initwithframe:cgrectmake (i * imagew, Imagey, Imagew, Imageh)];
Imageview.image = [UIImage imagenamed:[nsstring stringwithformat:@ "%i.jpg", i+1]];
[Self.scrollview Addsubview:imageview];
}
Self.scrollView.contentSize = Cgsizemake (kimagecount * imagew, 0);
Self.scrollView.delegate = self;
Self.pageControl.numberOfPages = Kimagecount;
[Self addtimer];
}
-(void) AddTimer {
Self.timer = [Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (nextimage) Userinfo:nil Repeats:yes];
[[Nsrunloop Mainrunloop]addtimer:self.timer Formode:nsdefaultrunloopmode];
}
-(void) nextimage{
int i = (int) self.pageControl.currentPage;
if (i = = KImageCount-1) {
i =-1;
}
i++;
[Self.scrollview setcontentoffset:cgpointmake (i * self.scrollView.frame.size.width, 0) Animated:yes];
}
-(void) scrollviewwillbegindragging: (Uiscrollview *) scrollview{
[Self turnofftimer];
}
-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{
[Self addtimer];
}
-(void) turnofftimer{
[Self.timer invalidate];
Self.timer = nil;
}
-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{
Self.pageControl.currentPage = (Self.scrollView.frame.size.width * 0.5 + self.scrollview.contentoffset.x)/ Self.scrollView.frame.size.width;
}
-(void) Viewdidload {
[Super Viewdidload];
Self.title = @ "Carousel graph";
[Self setview];
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS Carousel Diagram Implementation

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.