In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use iOS to achieve UIScrollView unlimited carousel function", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to use iOS to achieve UIScrollView unlimited carousel function" this article.
What is the UIScrollView control?
(1) the screen size of mobile devices is extremely limited, so the content displayed directly in front of users is also very limited.
(2) when the content of the exhibition is more than one screen, the user can view the content outside the screen by scrolling.
(3) ordinary UIView does not have scrolling function and cannot show too much content.
(4) UIScrollView is a scrolling view control that can be used to display a large amount of content and view all content by scrolling.
(5) examples: "Settings" on mobile phones and other sample programs
When it comes to UIScrollView, the first thing that comes to mind must be the unlimited rotation function on it. Apple does not provide a corresponding method for everyone to achieve rotation on UIScrollView, so it needs to be handled by code.
Let me first tell you about the principle of its implementation:
Let's assume that we use a few pictures to achieve the effect of rotation. First, we need to turn on the paging slide of UIScrollView
/ Paging slide _ scrollView.scrollEnabled = YES
It conveniently helps us to achieve the effect of rotation, and then we need to achieve "unlimited" rotation. Next, we need to put the picture, we need to pay attention when placing the picture, we need to put the last picture in the position of the first picture (maybe a little confused, but don't worry to read down slowly). Then we put the pictures in turn (from the first to the last), and finally we put the first picture at the end of all the pictures. In this way, we put two more pictures (one at the beginning and the other at the end). Let me write down the corresponding method:
/ / place the picture on the UIScrollView-(void) setupImage {/ add a picture UIImageView * firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake (0,0, kScreenWidth, self.scrollView.frame.size.height)] in front of the UIScrollView; / the picture name is the last picture firstImageView.image = [UIImage imageNamed:self.imageNameList.lastObject]; [self.scrollView addSubview:firstImageView]; / / add picture for (NSInteger index = 0; index < self.imageNameList.count) Index + +) {/ every picture on UIScrollView UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake ((index + 1) * kScreenWidth, 0, kScreenWidth, self.scrollView.frame.size.height)]; imageView.image = [UIImage imageNamed:self.imageNameList [index]]; [self.scrollView addSubview:imageView]; self.scrollView.contentSize = CGSizeMake ((index + 2) * self.scrollView.bounds.size.width, 0) } / / add a picture UIImageView * lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake ((self.imageNameList.count + 1) * kScreenWidth, 0, kScreenWidth, self.scrollView.frame.size.height)] at the end of the UIScrollView; / / the picture name is the first picture lastImageView.image = [UIImage imageNamed:self.imageNameList.firstObject]; [self.scrollView addSubview:lastImageView] / / set the offset of UIScrollView self.scrollView.contentSize = CGSizeMake ((self.imageNameList.count + 2) * self.scrollView.bounds.size.width, 0); / / set the starting offset distance of UIScrollView (skip the first picture) self.scrollView.contentOffset = CGPointMake (kScreenWidth, 0); / / the total number of pictures self.pageControl.numberOfPages = self.imageNameList.count; self.pageControl.currentPage = 0;}
In fact, if you see here, you should have a general understanding of the principle of wireless rotation. Next, the last step is to write the logic in UIScrollView's proxy method: determine the offset of UIScrollView. When it slips to the first position (showing the last picture), the slide stops, and the offset is changed to the position of the last image (the penultimate picture). Similarly, when the UIScrollView slides to the end (showing the first image), the slide stops and the offset is changed to the position of the first image (the second positive image).
# pragma mark-UIScrollViewDelegate- (void) scrollViewDidEndDecelerating: (UIScrollView *) scrollView {/ when UIScrollView slides to the first stop, change the offset position of UIScrollView to if (scrollView.contentOffset.x = = 0) {scrollView.contentOffset = CGPointMake (self.imageNameList.count * kScreenWidth, 0); self.pageControl.currentPage = self.imageNameList.count / / when UIScrollView slides to the last bit to stop, change the offset position of UIScrollView} else if (scrollView.contentOffset.x = = (self.imageNameList.count + 1) * kScreenWidth) {scrollView.contentOffset = CGPointMake (kScreenWidth, 0); self.pageControl.currentPage = 0;} else {self.pageControl.currentPage = scrollView.contentOffset.x / kScreenWidth-1;}}
Ok, that's how it works. Add two more pictures as placeholders at the beginning and end, and then when UIScrollView slides to the position of the placeholder, it is simple and convenient to change the offset of UIScrollView. Here is the whole code:
# import "ViewController.h" # define kScreenWidth [UIScreen mainScreen]. Bounds.size.width@interface ViewController () / / sliding controller @ property (nonatomic, strong) UIScrollView * scrollView;/// picture array @ property (nonatomic, strong) NSArray * imageNameList;/// page number controller @ property (nonatomic, strong) UIPageControl * pageControl;@end@implementation ViewController- (void) viewDidLoad {[super viewDidLoad] / / set the array of picture names self.imageNameList = @ [@ "image0", @ "image1", @ "image2", @ "image3"]; / / add a picture [self setupImage];} / / place the picture on the UIScrollView-(void) setupImage {/ add a picture UIImageView * firstImageView = [UIImageView alloc] initWithFrame:CGRectMake (0,0, kScreenWidth, self.scrollView.frame.size.height)] / / the picture name is the last picture firstImageView.image = [UIImage imageNamed:self.imageNameList.lastObject]; [self.scrollView addSubview:firstImageView]; / / add a picture for (NSInteger index = 0; index < self.imageNameList.count; index + +) {/ every picture on UIScrollView UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake ((index + 1) * kScreenWidth, 0, kScreenWidth, self.scrollView.frame.size.height)] ImageView.image = [UIImage imageNamed:self.imageNameList [index]]; [self.scrollView addSubview:imageView]; self.scrollView.contentSize = CGSizeMake ((index + 2) * self.scrollView.bounds.size.width, 0);} / / add a picture UIImageView * lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake ((self.imageNameList.count + 1) * kScreenWidth, 0, kScreenWidth, self.scrollView.frame.size.height)] at the end of the UIScrollView / / the picture name is the first picture lastImageView.image = [UIImage imageNamed:self.imageNameList.firstObject]; [self.scrollView addSubview:lastImageView]; / / set the offset of UIScrollView self.scrollView.contentSize = CGSizeMake ((self.imageNameList.count + 2) * self.scrollView.bounds.size.width, 0); / / set the starting offset distance of UIScrollView (skip the first picture) self.scrollView.contentOffset = CGPointMake (kScreenWidth, 0) / / Total number of pictures self.pageControl.numberOfPages = self.imageNameList.count; self.pageControl.currentPage = 0;} # pragma mark-UIScrollViewDelegate- (void) scrollViewDidEndDecelerating: (UIScrollView *) scrollView {/ when the UIScrollView slides to the first stop, change the offset position of the UIScrollView (scrollView.contentOffset.x = = 0) {scrollView.contentOffset = CGPointMake (self.imageNameList.count * kScreenWidth, 0); self.pageControl.currentPage = self.imageNameList.count / / when UIScrollView slides to the last bit to stop, change the offset position of UIScrollView} else if (scrollView.contentOffset.x = = (self.imageNameList.count + 1) * kScreenWidth) {scrollView.contentOffset = CGPointMake (kScreenWidth, 0); self.pageControl.currentPage = 0;} else {self.pageControl.currentPage = scrollView.contentOffset.x / kScreenWidth-1 } # pragma mark-Get method-(UIScrollView *) scrollView {if (! _ scrollView) {_ scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake (0,0, kScreenWidth, 200)]; _ scrollView.pagingEnabled = YES; _ scrollView.clipsToBounds = NO; _ scrollView.scrollEnabled = YES; _ scrollView.delegate = self; _ scrollView.bounces = NO; _ scrollView.showsHorizontalScrollIndicator = NO; _ scrollView.showsVerticalScrollIndicator = NO; [self.view addSubview:_scrollView];} return _ scrollView }-(UIPageControl *) pageControl {if (! _ pageControl) {_ pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake (0150, kScreenWidth, 50)]; _ pageControl.pageIndicatorTintColor = [UIColor blackColor]; _ pageControl.currentPageIndicatorTintColor = [UIColor grayColor]; [self.view addSubview:_pageControl];} return _ pageControl;} @ end
The above is all the content of the article "how to use iOS to achieve the unlimited rotation function of UIScrollView". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.