ScrollView implementation of Cyclic carousel

Source: Internet
Author: User

1 #import "MassageViewController.h"2 @interfaceMassageviewcontroller () <UIScrollViewDelegate>3@property (nonatomic,strong) Uiscrollview *Picscrollview;4@property (nonatomic,strong) Uipagecontrol *picctr;5@property (nonatomic,strong) Nstimer *timer;6@property (nonatomic,strong) Nsmutablearray *Picarr;7@property (nonatomic,strong) Uiimageview *Picimageview;8@property (nonatomic,strong) Uiimageview *Lastimageview;9 @endTen  One @implementationMassageviewcontroller A-(Nsmutablearray *) picarr{ -     if(self.picarr==Nil) { -_picarr =[Nsmutablearray array]; the     } -     return_picarr; - } -  +-(void) viewdidload{ -Self.picarr =[self loadimagedata]; +Self. Picscrollview = [[Uiscrollview alloc]initwithframe:cgrectmake (0, Max, Self.view.frame.size.width, $)]; A_picscrollview.pagingenabled =YES; at_picscrollview.Delegate=Self ; -_picscrollview.showshorizontalscrollindicator =NO; -_picscrollview.tag = +; -_picscrollview.contentsize = Cgsizemake (self. Picscrollview.width * (_picarr.count),0); -      for(inti =0; i<_picarr.count; i++) { -CGFloat IMGX = Self.view.frame.size.width *i; inSelf. Picimageview = [[Uiimageview alloc]initwithframe:cgrectmake (IMGX,0, Self.view.frame.size.width, $)]; -_picimageview.image =[_picarr objectatindex:i]; to [_picscrollview Addsubview:_picimageview]; +     } -CGFloat imgx = cgrectgetwidth (self.view.frame) *_picarr.count; the_lastimageview = [[Uiimageview alloc]initwithframe:cgrectmake (IMGX,0, Self.view.frame.size.width, $)]; *_lastimageview.image = [_picarr objectatindex:0]; $ [_picscrollview Addsubview:_lastimageview];Panax Notoginseng      -      the [Self.view Addsubview:_picscrollview]; +Self. PICCTR = [[Uipagecontrol Alloc]initwithframe:cgrectmake (self. picscrollview.width- Max)/2, self. picscrollview.y+ $- +, Max, -)]; A_picctr.pageindicatortintcolor =[Uicolor Graycolor]; the_picctr.currentpageindicatortintcolor =[Uicolor Redcolor]; +_picctr.numberofpages =5; -_picctr.currentpage =0; $ [Self.view addsubview:_picctr]; $ [Self.view bringsubviewtofront:_picctr]; -      -Self.timer = [Nstimer scheduledtimerwithtimeinterval:1.0target:self selector: @selector (scrollimage) Userinfo:nil Repeats:yes]; the     //Modify the priority level as other controls -     //gets the current message loop objectWuyiNsrunloop *runloop =[Nsrunloop Currentrunloop]; the     //setting the priority level - [Runloop AddTimer:self.timer formode:nsrunloopcommonmodes]; Wu } -  About-(Nsmutablearray *) loadimagedata{//loading picture $Nsmutablearray *arr =[Nsmutablearray array]; -      for(inti =0; i<5; i++) {
In order to save memory, it is recommended to use the path to load, note the picture should not be placed inside the assets.xcassets, otherwise read -NSString *imgname = [NSString stringWithFormat:@"%02d.jpg", i+1];//note. jpg format, image format Unified -NSString *imgpath =[[NSBundle mainbundle]pathforresource:imgname Oftype:nil]; AUIImage *image =[UIImage Imagewithcontentsoffile:imgpath]; + [arr addobject:image]; the } - returnarr; $ } the the-(uistatusbarstyle) preferredstatusbarstyle{ the returnuistatusbarstylelightcontent; the } - in-(void) Scrollviewwillbegindragging: (Uiscrollview *) scrollview{ the //Stop Timer the [Self.timer invalidate]; AboutSelf.timer =Nil; the } the the-(void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{ + - //R If put in Didscroll inside, each auto play will call, we want to drag the finger to call, so put in here, each time dragged to the first few, white point lit theCGFloat OffsetX = self. Picscrollview.contentoffset.x + self. picscrollview.frame.size.width/2;Bayi intindex = offsetx/Self . Picscrollview.width; the_picctr.currentpage =index; the - //re-enable timer -Self.timer = [Nstimer scheduledtimerwithtimeinterval:1.0target:self selector: @selector (scrollimage) Userinfo:nil Repeats:yes]; the //Modify the priority level as other controls the //gets the current message loop object theNsrunloop *runloop =[Nsrunloop Currentrunloop]; the //setting the priority level - [Runloop AddTimer:self.timer formode:nsrunloopcommonmodes]; the } the the-(void) scrollimage{94 //Add a ImageView behind the ScrollView, as in the first diagram. the floatOffsetX =_picscrollview.contentoffset.x; theOffsetX + =cgrectgetwidth (self.view.frame); the if(OffsetX = = Cgrectgetwidth (self.view.frame) *_picarr.count) {98_picctr.currentpage =0; About}Else{ -_picctr.currentpage = offsetx/cgrectgetwidth (self.view.frame);101 }102 if(Offsetx>cgrectgetwidth (self.view.frame) *_picarr.count) {103 //Direct copy speed is fast and lastimageview, just like a loop.104_picscrollview.contentoffset= Cgpointmake (0,0); the_picctr.currentpage =1;106[_picscrollview Setcontentoffset:cgpointmake (Cgrectgetwidth (Self.view.frame),0) Animated:yes];107}Else{108[_picscrollview Setcontentoffset:cgpointmake (OffsetX,0) Animated:yes];109 } the }111 @end

ScrollView implementation of Cyclic 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.