iOS Development UI Chapter-uiscrollview control implements picture Carousel

Source: Internet
Author: User

iOS Development UI Chapter-uiscrollview control implements picture Carousel

First, the realization effect

Auto-Carousel for images

Second, the implementation of the Code

Layout in Storyboard

Code:

1 #import "YYViewController.h"2 3 @interfaceYyviewcontroller () <UIScrollViewDelegate>4@property (Weak, nonatomic) Iboutlet Uiscrollview *ScrollView;5 /**6 * Page Number7  */8@property (Weak, nonatomic) Iboutlet Uipagecontrol *Pagecontrol;9 Ten@property (nonatomic, strong) Nstimer *timer; One @end A  - @implementationYyviewcontroller -  the- (void) Viewdidload - { - [Super Viewdidload]; -      + //the width of the picture -CGFloat Imagew =Self.scrollview.frame.size.width; + //cgfloat Imagew = +; A //Picture High atCGFloat Imageh =Self.scrollview.frame.size.height; - //the y of the picture -CGFloat Imagey =0; - //Number of pictures -Nsinteger TotalCount =5; - //1. Add 5 photos in      for(inti =0; i < TotalCount; i++) { -Uiimageview *imageview =[[Uiimageview alloc] init]; to //picture x +CGFloat ImageX = i *Imagew; - //Set Frame theImageview.frame =CGRectMake (ImageX, Imagey, Imagew, Imageh); * //set up a picture $NSString *name = [NSString stringWithFormat:@"img_0%d", i +1];Panax NotoginsengImageview.image =[UIImage imagenamed:name]; - //Hide the indicator bar theSelf.scrollview.showsHorizontalScrollIndicator =NO; +          A [Self.scrollview Addsubview:imageview]; the     } +      - //2. Set the scroll range of the ScrollView $CGFloat CONTENTW = TotalCount *Imagew; $     //do not allow scrolling in the vertical direction -Self.scrollview.contentSize = Cgsizemake (CONTENTW,0); -      the //3. Set Paging -self.scrollview.pagingEnabled =YES;Wuyi      the //4. Monitor the scrolling of ScrollView -Self.scrollview.Delegate=Self ; Wu      - [self addtimer]; About } $  -- (void) NextImage - { -     intpage = (int) Self.pageControl.currentPage; A     if(page = =4) { +page =0; the}Else -     { $page++; the     } the      the //scrolling ScrollView theCGFloat x = page *Self.scrollview.frame.size.width; -Self.scrollview.contentOffset = Cgpointmake (x,0); in } the  the //ScrollView when scrolling is called About- (void) Scrollviewdidscroll: (Uiscrollview *) ScrollView the { theNSLog (@"Scrolling in"); the //Calculate page Numbers + //page number = (Contentoffset.x + scrollview half width)/scrollview width -CGFloat SCROLLVIEWW =ScrollView.frame.size.width; theCGFloat x =Scrollview.contentoffset.x;Bayi     intpage = (x + scrollvieww/2) /Scrollvieww; theSelf.pageControl.currentPage =page; the } -  - //call when dragging is started the- (void) Scrollviewwillbegindragging: (Uiscrollview *) ScrollView the { the //Turn off the timer (note: Once the timer is closed, it can no longer be turned on) the //[Self.timer invalidate]; - [self removetimer]; the } the  the- (void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate94 { the //Turn on Timer the [self addtimer]; the }98  About /** - * Turn on the timer101  */102- (void) addtimer{103     104Self.timer = [Nstimer scheduledtimerwithtimeinterval:1target:self selector: @selector (nextimage) Userinfo:nil Repeats:yes]; the [[Nsrunloop Currentrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes];106 }107 /**108 * Turn off the timer109  */ the- (void) Removetimer111 { the [Self.timer invalidate];113 } the @end

Tip: The following two properties have been wired to the controls in storyboard.

@property (weak, nonatomic) iboutletuiscrollview *scrollview;

@property (weak, nonatomic) iboutletuipagecontrol *pagecontrol;

Supplement: timer Nstimer

Timers are designed to be used at intervals of time to do some long-spaced operations.

Nstimeinterval: How long and how many pieces to operate once

Target: Who to manipulate

Selector: The method to be manipulated

UserInfo: Passing Parameters

Repeats: whether to repeat

Self.timer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (nextimage) Userinfo:nil repeats : YES];

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.