Simple unlimited image carousel and unlimited carousel

Source: Internet
Author: User

Simple unlimited image carousel and unlimited carousel

1 # import "ViewController. h "2 # define kDeviceWidth [UIScreen mainScreen]. bounds. size. width 3 # define kDeviceHeight [UIScreen mainScreen]. bounds. size. height 4 # define Kpage 3 5 @ interface ViewController () <UIScrollViewDelegate> 6 7 @ property (nonatomic, strong) UIPageControl * pageCtrl; 8 @ property (nonatomic, weak) UIScrollView * scrollView; 9 @ property (nonatomic, weak) UIImageView * currentImageView ;// Current imageView 10 @ property (nonatomic, weak) UIImageView * nextImageView; // The next imageView 11 @ property (nonatomic, weak) UIImageView * preImageView; // previous imageView 12 @ property (nonatomic, assign) BOOL isDragging; // whether to drag 13 @ property (nonatomic, strong) NSTimer * timer; 14 @ end 15 16 @ implementation ViewController 17 18-(void) viewDidLoad {19 [super viewDidLoad]; 20 UIScrollView * scrollView = [UIScrol LView alloc] init]; 21 scrollView. frame = CGRectMake (0, 0, kDeviceWidth, kDeviceHeight); 22 [self. view addSubview: scrollView]; 23 self. scrollView = scrollView; 24 [self. scrollView setContentSize: CGSizeMake (kDeviceWidth * 3, kDeviceWidth)]; 25 // set to hide the horizontal bar 26 self. scrollView. showsHorizontalScrollIndicator = NO; 27 // set Automatic paging 28 self. scrollView. pagingEnabled = YES; 29 // set proxy 30 self. scrollView. delegat E = self; 31 // set the current vertex 32 self. scrollView. contentOffset = CGPointMake (kDeviceWidth, 0); 33 // set whether there is a boundary 34 self. scrollView. bounces = NO; 35 // initialize the current view 36 UIImageView * currentImageView = [[UIImageView alloc] init]; 37 currentImageView. image = [UIImage imageNamed: @ "bg_01"]; 38 [self. scrollView addSubview: currentImageView]; 39 self. currentImageView = currentImageView; 40 self. currentImageView. frame = CGR EctMake (kDeviceWidth, 0, kDeviceWidth, kDeviceHeight); 41 self. currentImageView. contentMode = UIViewContentModeScaleAspectFill; 42 // initialize the next view 43 UIImageView * nextImageView = [[UIImageView alloc] init]; 44 nextImageView. image = [UIImage imageNamed: @ "bg_02"]; 45 [self. scrollView addSubview: nextImageView]; 46 self. nextImageView = nextImageView; 47 self. nextImageView. frame = CGRectMake (kDeviceWidth * 2, 0, kDeviceWidth, kDeviceHeight); 48 self. nextImageView. contentMode = UIViewContentModeScaleAspectFill; 49 // initialize the previous view 50 UIImageView * preImageView = [[UIImageView alloc] init]; 51 preImageView. image = [UIImage imageNamed: @ "bg_03"]; 52 preImageView. frame = CGRectMake (0, 0, kDeviceWidth, kDeviceHeight); 53 [self. scrollView addSubview: preImageView]; 54 self. preImageView = preImageView; 55 self. pre ImageView. contentMode = UIViewContentModeScaleAspectFill; 56 57 // set the clock animation timer 58 59 self. timer = [NSTimer scheduledTimerWithTimeInterval: 2 target: self selector: @ selector (update :) userInfo: nil repeats: YES]; 60 // Add the timer to the main thread 61 [[[nsunloop mainRunLoop] addTimer: self. timer forMode: nsunloopcommonmodes]; 62 63} 64 // page 65-(UIPageControl *) pageCtrl {66 if (_ pageCtrl = nil) {67 68 // paging control 69 _ pageCtr L = [[UIPageControl alloc] init]; 70 _ pageCtrl. numberOfPages = Kpage; 71 72 CGSize size = [_ pageCtrl sizeForNumberOfPages: Kpage]; 73 _ pageCtrl. bounds = CGRectMake (0, 0, size. width, size. height); 74 _ pageCtrl. center = CGPointMake (self. view. center. x, CGRectGetMaxY (self. scrollView. frame)-20); 75 _ pageCtrl. pageIndicatorTintColor = [UIColor redColor]; 76 _ pageCtrl. currentPageIndicatorTintColor = [UICo Lor greenColor]; 77 78 [self. view addSubview: _ pageCtrl]; 79 80 81} 82 return _ pageCtrl; 83} 84 85 86-(void) updateTimer {87 // modify page number 88 int page = (self. pageCtrl. currentPage + 1) % Kpage; 89 self. pageCtrl. currentPage = page; 90 91} 92 93 94-(void) update :( NSTimer *) timer {95 // regularly move 96 97 if (_ isDragging = YES) {98 99 return; 100} 101 CGPoint offSet = self. scrollView. contentOffset; 102 offSet. x + = OffSet. x; 103 104 [self updateTimer]; 105 106 [self. scrollView setContentOffset: offSet Animation: YES]; 107 if (offSet. x> = kDeviceWidth x 2) {108 offSet. x = kDeviceWidth; 109} 110 111} 112 # pragma mark-UIScrollViewDelegate113-(void) scrollViewWillBeginDragging :( UIScrollView *) scrollView114 {115 _ isDragging = YES; 116} 117 // stop rolling 118-(void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView119 {120 _ isDragging = NO; 121 122} 123 124 // start dragging 125-(void) scrollViewDidScroll :( UIScrollView *) scrollView {126 static int I = 1; // currently, the number of images displayed is 127 float offset = self. scrollView. contentOffset. x; 128 if (self. nextImageView. image = nil | self. preImageView. image = nil) {129 // load the next view 130 NSString * imageName1 = [NSString stringWithFormat: @ "bg_0% d", I = Kpage? 1: I + 1]; 131 _ nextImageView. image = [UIImage imageNamed: imageName1]; 132 // load the last view 133 NSString * imageName2 = [NSString stringWithFormat: @ "bg_0% d", I = 1? Kpage: I-1]; 134 _ preImageView. image = [UIImage imageNamed: imageName2]; 135 136} 137 if (offset = 0) {138 _ currentImageView. image = _ preImageView. image; 139 scrollView. contentOffset = CGPointMake (scrollView. bounds. size. width, 0); 140 _ preImageView. image = nil; 141 if (I = 1) {142 I = Kpage; 143} else {144 I-= 1; 145} 146 147} 148 if (offset = scrollView. bounds. size. width x 2) {149 _ currentImageView. image = _ nextImageView. image; 150 scrollView. contentOffset = CGPointMake (scrollView. bounds. size. width, 0); 151 _ nextImageView. image = nil; 152 if (I = Kpage) {153 I = 1; 154} else {155 I + = 1; 156} 157 158} 159 160} 161 162 163 @ end

Source file here: http://pan.baidu.com/s/1kVKrbkb

There is also a good third party: http://pan.baidu.com/s/1kUBTsZ9

If not, search for SDCycleScrollView-master in github.com.

Related Article

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.