iOS Learning-scrollView (Photo carousel)

Source: Internet
Author: User

yjviewcontroller.m//07-Picture Carousel////Created by Jacky-mac on 15-6-18.//Copyright (c) 2015 www.train.com. All rights reserved.//#import "YJViewController.h" #define Kimagecount 5@interface Yjviewcontroller () < Uiscrollviewdelegate> @property (Nonatomic,strong) Uiscrollview *scrollview; @property (Nonatomic,strong) Uipagecontrol *pagecontrol, @property (nonatomic,strong) Nstimer *timer; @end @implementation yjviewcontroller-( Uiscrollview *) scrollview{if (_scrollview ==nil) {_scrollview = [[Uiscrollview alloc] Initwithframe:cgrectmake        (10, 20, 300, 130)];                _scrollview.backgroundcolor = [Uicolor Redcolor];                   [Self.view Addsubview:_scrollview];                   Cancel the Spring effect _scrollview.bounces = NO;        Remove horizontal ScrollBar _scrollview.showshorizontalscrollindicator = NO;                _scrollview.showsverticalscrollindicator = NO;               to paging _scrollview.pagingenabled = YES; Contentsize _scrollview.contentsiZe = cgsizemake (kimagecount * _scrollview.bounds.size.width, 0);    Set proxy _scrollview.delegate = self; } return _scrollview;}  -(Uipagecontrol *) pagecontrol{if (_pagecontrol = = nil) {//pagination control, essentially without any relationship to ScrollView, is two separate controls _pagecontrol                      = [[Uipagecontrol alloc] init];                  Total pages _pagecontrol.numberofpages = Kimagecount;                Control size Cgsize size = [_pagecontrol sizefornumberofpages:kimagecount];        _pagecontrol.bounds = CGRectMake (0, 0, size.width, size.height);                     _pagecontrol.center = Cgpointmake (self.view.center.x, 130);        Set color _pagecontrol.pageindicatortintcolor = [Uicolor Redcolor];                _pagecontrol.currentpageindicatortintcolor = [Uicolor blackcolor];             [Self.view Addsubview:_pagecontrol]; Adding a listening method/** in OC, the vast majority of "controls" can listen for uicontroleventvaluechanged events, except for button "/[_pagecontrol Addtarget:se LF Action: @selectoR (pagechanged:) forcontrolevents:uicontroleventvaluechanged]; } return _pagecontrol;}        The paging control's listener Method-(void) pagechanged:(Uipagecontrol *) page{NSLog (@ "%d", page.currentpage);    Adjusts the position of the picture in the scrolling view based on the number of pages Contentoffset cgfloat x = page.currentpage * self.scrollView.bounds.size.width;    [Self.scrollview setcontentoffset:cgpointmake (x, 0) Animated:yes]; }-(void) viewdidload{[super viewdidload];//Set picture for (int i = 0; i < Kimagecount; i++) {NSString *image        Name = [NSString stringwithformat:@ "img_%02d", i+1];                UIImage *image = [UIImage imagenamed:imagename];        Uiimageview *imageview = [[Uiimageview alloc] initWithFrame:self.scrollView.bounds];                Imageview.image = image;    [Self.scrollview Addsubview:imageview]; }//Calculates the location of the ImageView [Self.scrollView.subviews enumerateobjectsusingblock:^ (Uiimageview *imageview, Nsuinteger idx, BOOL *stop) {//Adjustment X = origin = frame CGRect frame = Imageview.fraMe                frame.origin.x = idx * frame.size.width;    Imageview.frame = frame;            }];            The initial page number of pages is 0 self.pageControl.currentPage = 0; Start clock [self starttimer];} -(void) starttimer{Self.timer = [Nstimer scheduledtimerwithtimeinterval:2.0 target:self selector: @selector ( Updatetimer) Userinfo:nil Repeats:yes];}    -(void) updatetimer{//page number changed//(current page + 1)% Total pages int page = (self.pageControl.currentPage + 1)% Kimagecount;          self.pageControl.currentPage = page; Invoke the listener method to let scrolling view scroll [self PageChanged:self.pageControl];} #pragma Mark-scrollview Proxy method//Scrolling view stop, modify the page control's dots (pages)-(void) scrollviewdidenddecelerating: (Uiscrollview *)            scrollview{//Stop Current page NSLog (@ "%@", Nsstringfromcgpoint (Scrollview.contentoffset));        Count pages int page = Scrollview.contentoffset.x/scrollview.bounds.size.width;    self.pageControl.currentPage = page; } @end


iOS Learning-scrollView (Photo carousel)

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.