UIScrollView: 1. uiscrollview:

Source: Internet
Author: User

UIScrollView: 1. uiscrollview:

Now, basically every commercial APP has a circular scroll view, where some carousel advertisements and the like are placed on UIScrollView. If I want to implement N images carousel, I have used several blog posts to get two methods:

[Option 1]: for example (images are from the kenshin cui's blog). If you want to rotate three images, set imageView on UIScrollView to 3 + 2, the image sequence is 31231.

When the UIScrollView is initially loaded, it is rolled to the second imageView.

When you scroll to the last imageview, set the ContentOffset of UIScrollView to the second imageView.

When you scroll forward to the first imageview, set the ContentOffset of UIScrollView to the last and second imageview.

The Code is as follows:

1 # import "ViewController. h "2 // define macro to get screen width and height 3 # define SCREEN_WIDTH [UIScreen mainScreen]. bounds. size. width 4 # define SCREEN_HEIGHT [UIScreen mainScreen]. bounds. size. height 5 6 @ interface ViewController () <UIScrollViewDelegate> 7/** rolling view */8 @ property (nonatomic, strong) UIScrollView * scrollView; 9/** Image array */10 @ property (nonatomic, strong) NSMutableArray * imageArray; 11 @ end12 13 @ implementation ViewCo Ntroller14 // lazy loading scroll view 15-(UIScrollView *) scrollView {16 if (! _ ScrollView) {17 _ scrollView = [[UIScrollView alloc] init]; 18} 19 return _ scrollView; 20} 21 // lazy loading Image array 22-(NSMutableArray *) imageArray {23 if (! _ ImageArray) {24 _ imageArray = [NSMutableArray array]; 25} 26 return _ imageArray; 27} 28 29 30-(void) viewDidLoad {31 [super viewDidLoad]; 32 // first load the image 33 [self loadImageArray]; 34 // then set the scroll view 35 [self setupScrollview]; 36} 37 // load the image, suppose there are three images: welcome1 welcome2 welcome338-(void) loadImageArray {39 for (NSInteger I = 0; I <3; I ++) {40 UIImage * image = [UIImage imageNamed: [NSString stringWithFormat: @ "welcome % ld", I + 1]; 41 [self. imageArray addObject: image]; 42} 43} 44 // set scrollview45-(void) setupScrollview {46 // set the visible area 47 self. scrollView. frame = self. view. bounds; 48 // set the capacity to 49 self. scrollView. contentSize = CGSizeMake (SCREEN_WIDTH * (self. imageArray. count + 2), SCREEN_HEIGHT); 50 // paging mode 51 self. scrollView. pagingEnabled = YES; 52 // proxy 53 self. scrollView. delegate = self; 54 // The Edge cannot bounce 55 self. scrollView. bounces = NO; 56 // hide the horizontal scroll bar 57 self. scrollView. showsHorizontalScrollIndicator = NO; 58 [self. view addSubview: self. scrollView]; 59 // Add imageView60 for (NSInteger I = 0; I <self. imageArray. count + 2; I ++) {61 UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (I * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; 62 if (I = 0) {// 0-> lastObject63 imageView. image = self. imageArray. lastObject; 64} else if (I = self. imageArray. count + 1) {// 4-> firstObject65 imageView. image = self. imageArray. firstObject; 66} else {// 1-> 0 2-> 1 3-> 267 imageView. image = self. imageArray [I-1]; 68} 69 [self. scrollView addSubview: imageView]; 70} 71 // set the initial scroll position to the second imageView72 self. scrollView. contentOffset = CGPointMake (SCREEN_WIDTH, 0); 73} 74 // when the scroll is stopped, reset the ContentOffset75-(void) scrollViewDidEndDecelerating :( UIScrollView *) of the scrollview *) scrollView {76 CGPoint point = scrollView. contentOffset; 77 if (point. x/SCREEN_WIDTH> self. imageArray. count) {// 78 [self. scrollView setContentOffset: CGPointMake (SCREEN_WIDTH, 0) animated: NO]; 79} else if (point. x/SCREEN_WIDTH <1) {// 80 [self. scrollView setContentOffset: CGPointMake (SCREEN_WIDTH * self. imageArray. count, 0) animated: NO]; 81} 82}

You can create a new project and copy the Code directly. In the loadImageArray method, modify it to load your own image and run it.

[2]: if there are 100 images or more, you have to load 100 + 2 imageviews. Obviously, the performance requirement is higher, so the second type is derived.

That is, no matter how many images you have, there are only three imageview images.

No matter how you scroll, when the scroll is complete, the ContentOffset of UIScrollView is an imageview in the middle.

We only need to change the image of each imageview. image in the UIScrollView proxy method.

The second source code will be added in subsequent blog posts.

In fact, this loop rolling learning is not a way of thinking, but a way of thinking.

Finally, we recommend a third-party class library SDCycleScrollView which is very useful for github. It has more than two thousand stars and is quite good.

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.