iOS picture Carousel

Source: Internet
Author: User

Directly on the code, relatively simple. Demo: Demo

uyviewcontroller.m//Picture Carousel////Created by Jiangys on 15/5/23.//Copyright (c) 2015 Uxiaoyuan. All rights reserved.//#import "UYViewController.h" #define Kimagecount 5@interface Uyviewcontroller () < Uiscrollviewdelegate> @property (Nonatomic,strong) Uiscrollview *scrollview; @property (Nonatomic,strong) Uipagecontrol *pagecontrol; @property (nonatomic, strong) Nstimer *timer; @end @implementation uyviewcontroller-( Uiscrollview *) scrollview{if (_scrollview==nil) {//if blank, create a _scrollview=[[uiscrollview alloc] Initwith        Frame:cgrectmake (10, 20, 300, 130)];                _scrollview.backgroundcolor=[uicolor Redcolor];                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;                         [Self.view Addsubview:_scrollview]; } return _scrollview;} -(Uipagecontrol *) pagecontrol{if (_pagecontrol = = nil) {//pagination control, essentially without any relationship to ScrollView, is two separate controls _pagecon        Trol = [[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:self ACTI On: @selector (PagechaNged:) forcontrolevents:uicontroleventvaluechanged]; } return _pagecontrol;} How to listen for paging controls-(void) pagechanged: (Uipagecontrol *) page{//Depending on the number of pages, adjust the position of the picture in the scrolling view contentoffset cgfloat x = page.currentpa    GE * 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 *imagename=[n        Sstring 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]; }//Calculate 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 timerwithtimeinterval:2.0 target:self selector: @selector (Updatetimer) UserI    Nfo:nil Repeats:yes]; Add to run loop [[[Nsrunloop Currentrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes];}    -(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];}    -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be Recreated.s} #pragma mark-scrollview proxy method//Scroll view to stop, modify the page control's dots (pages)-(void) Scro Llviewdidenddecelerating: (Uiscrollview *) scrollview{//Calculate page int page = Scrollview.contentoffset.x/scrollview.boun        Ds.size.width; self.pageControl.currentPage = page;} /** Modify the mode of the running loop where the clock is located, cannot grasp the picture solution: When you grab the picture, stop the clock, sendAfter sale, turn on the clock */-(void) scrollviewwillbegindragging: (Uiscrollview *) scrollview{//Stop the clock, after the stop can no longer use, if you want to enable the clock, you need to re-instantiate [self.t Imer invalidate];} -(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{[self starttimer];} @end

iOS picture 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.