iOS developing picture carousel and timer minor issues

Source: Internet
Author: User

One: The image of the carousel effect: To achieve automatic rotation, to the last page, the carousel back, you can implement drag-and-drop scrolling

Two: Code:

1 #import "ViewController.h"2 StaticCGFloatConstImagecount =5;3 @interfaceViewcontroller () <UIScrollViewDelegate>4 /*ScrollView*/5@property (nonatomic,weak) Uiscrollview *Scroll;6 /*Pagecontrol*/7@property (nonatomic,weak) Uipagecontrol *Pagecontrol;8 /*Timer*/9@property (nonatomic, strong) Nstimer *timer;Ten@property (nonatomic,assign)intA; One @end A  - @implementationViewcontroller -  the- (void) Viewdidload { - [Super Viewdidload]; -      -     //0: Create bottom View +UIView *bottomview =[[UIView alloc]init]; -Bottomview.frame = CGRectMake ( -, -, Self.view.frame.size.width- +, $); + [Self.view Addsubview:bottomview]; A      at      -     //1: Create a scrolling view -Uiscrollview *scrollview =[[Uiscrollview alloc]init]; -Scrollview.frame =bottomview.bounds; -Scrollview.bounces =NO; -Scrollview.showsverticalscrollindicator =NO; inScrollview.showshorizontalscrollindicator =NO; -scrollview.pagingenabled =YES; toScrollView.Delegate=Self ; +Scrollview.backgroundcolor =[Uicolor Redcolor]; -Self.scroll =ScrollView; the [Bottomview Addsubview:scrollview]; *      $     //2: Create ImageViewPanax NotoginsengCGFloat imagewidth =ScrollView.frame.size.width; -CGFloat imageheight =ScrollView.frame.size.height; the      +      for(inti =0; i < Imagecount; i++) { A          theUiimageview *imageview =[[Uiimageview alloc]init]; +Imageview.frame = CGRectMake (i *imagewidth,0, ImageWidth, imageheight); -Imageview.image = [UIImage imagenamed:[nsstring stringWithFormat:@"Img_0%d.png", i+1]]; $ [ScrollView Addsubview:imageview]; $          -     } -      the     //3: Set Scroll's contentsize -Scrollview.contentsize = Cgsizemake (Imagecount *imagewidth,0);Wuyi      the      -     //4: Create Pagecontrol WuUipagecontrol *pagecontrol =[[Uipagecontrol alloc]init]; -Pagecontrol.numberofpages =Imagecount; AboutPagecontrol.currentpage =0; $Pagecontrol.pageindicatortintcolor =[Uicolor Whitecolor]; -Pagecontrol.currentpageindicatortintcolor =[Uicolor Redcolor]; -Pagecontrol.center = Cgpointmake (BottomView.frame.size.width *0.5, BottomView.frame.size.height *0.9); -Self.pagecontrol =Pagecontrol; A [Bottomview Addsubview:pagecontrol]; +      the     //5: Add timer -Nstimer *timer = [Nstimer timerwithtimeinterval:2target:self selector: @selector (changeimage) Userinfo:nil Repeats:yes]; $Nsrunloop *runloop =[Nsrunloop Currentrunloop]; the     /* the parameters of the Formode: the Nsdefaultrunloopmode the Nsrunloopcommonmodes -      */ inSelf.timer =timer; the [Runloop Addtimer:timer formode:nsrunloopcommonmodes]; the //[timer fire]; About      the      the     //6: Add Uitextview theUitextview *textview =[[Uitextview alloc]init]; +Textview.frame = CGRectMake (0, the, Self.view.frame.size.width, Max); -Textview.text =@"all things look at the big Buddha I am Assad i ah search ID Buddha love you put that ship idif finish strengthen lease go in I ijefo everything to see big Buddha I Assad fo i ah search id Buddha love you all put that idif finish strengthen lease go in I ijefo everything to see big Buddha I Assad fo i ah search ID Buddhist Love you all put that ship idif finish strengthening Lease inside I ijefo everything to see the big Buddha I Assad fo i ah search id Buddha love you all put that idif finish strengthen lease go in I ijefo everything to see big Buddha I Assad fo i ah search ID Buddhist Love you all put that idif finish strengthen lease in I ijefo everything to see big Buddha I Assad fo i ah search ID Buddhist Love you put that ID If finish strengthens lease go inside I ijefo everything to see big Buddha I am Assad fo i ah search id Buddha love you all put that idif finish strengthen lease go in I ijefo everything to see big Buddha I Assad fo i ah search ID Buddhist Love you all put that idif finish strengthen lease go in I ijefo everything to see big Buddha I am I am Put that ship idif finish strengthen lease go in I ijefo everything to see the big Buddha I am Assad fo i ah search ID Buddhist Love you all put that ship idif finish strengthen lease go in I ijefo"; thetextview.scrollenabled =YES;BayiTextview.backgroundcolor =[Uicolor Redcolor]; the [Self.view Addsubview:textview]; the } -  -- (void) Changeimage { the      theNsinteger page =Self.pageControl.currentPage; the  the      -      the     if(page = = Imagecount-1) { theSELF.A = Imagecount-1; the     }94      the      the     if(page = =0) { theSELF.A =0;98     } About      - 101     if(SELF.A = = Imagecount-1) {102         103Page-- ;104         the}Else {106         107Page + +;108     }109      the 111 //[self.scroll setcontentoffset:cgpointmake (page *self.scroll.frame.size.width, 0) Animated:yes]; the[UIView animatewithduration:1.0animations:^{113Self.scroll.contentOffset = cgpointmake (page *self.scroll.frame.size.width,0); the     }]; the      the }117 118- (void) Scrollviewdidscroll: (Uiscrollview *) ScrollView {119      -         //when the next page slides to the >=0.5 width, it's time to change Pagecontrol121CGFloat page = scrollview.contentoffset.x/ScrollView.frame.size.width;122Nsuinteger currentpage =page;123Self.pageControl.currentPage = (Page-currentpage) <0.5? Currentpage:currentpage +1;124 } the 126 //turn off the timer at the start of the drag, and once the timer is off, it cannot be turned on again127- (void) Scrollviewwillbegindragging: (Uiscrollview *) ScrollView { - [Self.timer invalidate];129  the }131  the- (void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate {133     134Self.timer = [Nstimer scheduledtimerwithtimeinterval:2target:self selector: @selector (changeimage) Userinfo:nil Repeats:yes];135 }136 137 138 @end

Three: summary of knowledge points

1: Create timer: 1:nstimer *timer = [Nstimer timerwithtimeinterval:2 target:self selector: @selector (changeimage) Userinfo:nil Repeats:yes]; The timer created by this method must be added to Nsrunloop, nsrunloop *runloop = [Nsrunloop currentrunloop];  [Runloop Addtimer:timer Formode:nsrunloopcommonmodes]; the second parameter has two types to choose from: the Formode parameter: Nsdefaultrunloopmode Nsrunloopcommonmodes, the first parameter is the default parameter, when there are controls such as Textview,textfield, drag the control, when the carousel stops the carousel, because Nsrunloop reason, nsrunloop for a dead loop, Real-time monitoring of event response, TextView or TextField events will affect the picture carousel, using the second parameter will solve the problem 2:self.timer = [Nstimer scheduledtimerwithtimeinterval:2 Target:self selector: @selector (changeimage) Userinfo:nil Repeats:yes]; This method of creating a timer is added by default to Runloop, and the default is the second parameter. 3: Timer method: [Timer fire], the method of executing the timer immediately, if this method is not called, it will wait 2 seconds to execute the timer method.

2: Carousel Drag and drop: in the drag-and-drop carousel, does not affect the carousel effect, the implementation of the rolling agent start drag and drop method, and turn off the timer [Self.timer invalidate]; Once the timer is closed, it cannot be re-opened and the timer needs to be recreated. At the end of the drag-and-drop agent, re-create the timer

3: The picture is scrolled over the width of 0.5 when the calculation method: Rounding CGFloat page = Scrollview.contentoffset.x/scrollview.frame.size.width; Nsuinteger currentpage = page; Self.pageControl.currentPage = (page-currentpage) < 0.5? Currentpage:currentpage +1; First find the floating number of the scroll, in the conversion to an integer, do the difference, less than 0.5 to take the current integer, greater than 0.5 to take the current integer plus 1

4: Set offset for ScrollView: 1:uiview animation 2:[self.scroll setcontentoffset:cgpointmake (page *self.scroll.frame.size.width, 0) Animated:yes]; The second method, scrolling time is short, time is less than 1 seconds, the first is the time to control the animation, and the completion of the animation callback

5: Set the Carousel effect:

Nsinteger page = self.pageControl.currentPage;

if (page = = imageCount-1) {

SELF.A = imageCount-1;

}

if (page = = 0) {

SELF.A = 0;

}

if (SELF.A = = imageCount-1) {

Page--;

}else {

Page + +;

}

Note: Do not set the currentpage of Pagecontrol in this method, because if set, it will be immediately offset to the corresponding position, but at this time also in real-time monitoring scrollview scrolling, in this scroll agent, less than half the width, will take the current integer page, Greater than will take an integer + 1 pages, so at this point Pagecontrol will immediately go to a page, back to 1 pages, and then suddenly back to a page, there is a bug. Do not set the Pagecontrol currentpage, let it directly in the scroll agent to set.

iOS developing picture carousel and timer minor issues

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.