重要的点
在xib文件设置中关闭垂直和水平方向的滚动条
在xib中拖拽设置代理:scrollView的代理设置为自己的父控件;代理是谁,谁通过拖线完成的;
添加自己的项目前缀;
page基础
将图片放到了NArray中文件中读取
#import "ViewController.h" @interface ViewController () <UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @property (weak, nonatomic) IBOutlet UIPageControl *pageControl; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; int count = 5; CGFloat w = self.scrollView.frame.size.width; CGFloat h = self.scrollView.frame.size.height; for (int i = 0; i<count; i++) { // 创建 UIImageView *imageView = [[UIImageView alloc] init]; NSString *name = [NSString stringWithFormat:@"img_0%d", i]; imageView.image = [UIImage imageNamed:name]; // frame imageView.frame = CGRectMake(i * w, 0, w, h); [self.scrollView addSubview:imageView]; } // 设置内容大小 CGFloat contentW = count * w; self.scrollView.contentSize = CGSizeMake(contentW, 0); self.scrollView.showsHorizontalScrollIndicator = NO; // 开启分页功能 self.scrollView.pagingEnabled = YES; // 总页数 self.pageControl.numberOfPages = count; } #pragma mark - <UIScrollViewDelegate> /** * 只要scrollView在滚动,就会调用 */ - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 0.3 > (int)(0.3 + 0.5) > 0 // 0.6 > (int)(0.6 + 0.5) > 1 // 小数四舍五入为整数 : (int)(小数 + 0.5) int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5); self.pageControl.currentPage = page; } @end
#import <UIKit/UIKit.h> @interface XMGPageView : UIView + (instancetype)pageView; /** 图片名字 */ @property (nonatomic, strong) NSArray *imageNames; @end
// // XMGPageView.m // 08-分页 #import "XMGPageView.h" @interface XMGPageView() @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @property (weak, nonatomic) IBOutlet UIPageControl *pageControl; @end @implementation XMGPageView + (instancetype)pageView { return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject]; } - (void)setImageNames:(NSArray *)imageNames { _imageNames = imageNames; for (int i = 0; i<imageNames.count; i++) { UIImageView *imageView = [[UIImageView alloc] init]; imageView.image = [UIImage imageNamed:imageNames[i]]; [self.scrollView addSubview:imageView]; } } - (void)layoutSubviews { [super layoutSubviews]; // TODO 设置所有imageView的frame } @end
包装
#import "ViewController.h" #import "XMGPageView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; XMGPageView *pageView = [XMGPageView pageView]; pageView.frame = CGRectMake(37, 50, 300, 130); pageView.imageNames = @[@"img_00", @"img_01", @"img_02", @"img_03", @"img_04"]; [self.view addSubview:pageView]; } @end
寄己封装的view
XMGPageView.h
#import <UIKit/UIKit.h> @interface XMGPageView : UIView + (instancetype)pageView; /** 图片名字 */ @property (nonatomic, strong) NSArray *imageNames; @end
XMGPageView.m
// // XMGPageView.m // 08-分页 #import "XMGPageView.h" @interface XMGPageView() @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @property (weak, nonatomic) IBOutlet UIPageControl *pageControl; @end @implementation XMGPageView + (instancetype)pageView { return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject]; } - (void)setImageNames:(NSArray *)imageNames { _imageNames = imageNames; for (int i = 0; i<imageNames.count; i++) { UIImageView *imageView = [[UIImageView alloc] init]; imageView.image = [UIImage imageNamed:imageNames[i]]; [self.scrollView addSubview:imageView]; } } - (void)layoutSubviews { [super layoutSubviews]; // TODO 设置所有imageView的frame } @end
封装属性增加(接口增强)
// // ViewController.m // 08-分页 #import "ViewController.h" #import "XMGPageView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; XMGPageView *pageView = [XMGPageView pageView]; pageView.frame = CGRectMake(37, 50, 250, 200); pageView.imageNames = @[@"img_00", @"img_01", @"img_02"]; pageView.otherColor = [UIColor grayColor]; pageView.currentColor = [UIColor orangeColor]; pageView.currentColor = [UIColor blueColor]; [self.view addSubview:pageView]; pageView.imageNames = @[@"img_00", @"img_01", @"img_02", @"img_03", @"img_04"]; // pageView.imageNames = @[@"img_00"]; // pageView.frame = CGRectMake(0, 0, 100, 100); } @end
// XMGPageView.h // 08-分页 #import <UIKit/UIKit.h> @interface XMGPageView : UIView + (instancetype)pageView; /** 图片名字 */ @property (nonatomic, strong) NSArray *imageNames; /** 其他圆点颜色 */ @property (nonatomic, strong) UIColor *otherColor; /** 当前圆点颜色 */ @property (nonatomic, strong) UIColor *currentColor; @end
// // XMGPageView.m // 08-分页 #import "XMGPageView.h" @interface XMGPageView() <UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @property (weak, nonatomic) IBOutlet UIPageControl *pageControl; @end @implementation XMGPageView + (instancetype)pageView { return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject]; } - (void)layoutSubviews { [super layoutSubviews]; // 设置scrollView的frame self.scrollView.frame = self.bounds; // 获得scrollview的尺寸 CGFloat scrollW = self.scrollView.frame.size.width; CGFloat scrollH = self.scrollView.frame.size.height; // 设置pageControl CGFloat pageW = 100; CGFloat pageH = 20; CGFloat pageX = scrollW - pageW; CGFloat pageY = scrollH - pageH; self.pageControl.frame = CGRectMake(pageX, pageY, pageW, pageH); // 设置内容大小 self.scrollView.contentSize = CGSizeMake(self.imageNames.count * scrollW, 0); // 设置所有imageView的frame for (int i = 0; i<self.scrollView.subviews.count; i++) { UIImageView *imageView = self.scrollView.subviews[i]; imageView.frame = CGRectMake(i * scrollW, 0, scrollW, scrollH); } } #pragma mark - setter方法的重写 - (void)setImageNames:(NSArray *)imageNames { _imageNames = imageNames; // 移除之前的所有imageView // 让subviews数组中的所有对象都执行removeFromSuperview方法 [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; // 根据图片名创建对应个数的imageView for (int i = 0; i<imageNames.count; i++) { UIImageView *imageView = [[UIImageView alloc] init]; imageView.image = [UIImage imageNamed:imageNames[i]]; [self.scrollView addSubview:imageView]; } // 设置总页数 self.pageControl.numberOfPages = imageNames.count; // if (imageNames.count <= 1) { // self.pageControl.hidden = YES; // } else { // self.pageControl.hidden = NO; // } // self.pageControl.hidden = imageNames.count <= 1; // self.pageControl.hidesForSinglePage = YES; } - (void)setCurrentColor:(UIColor *)currentColor { _currentColor = currentColor; self.pageControl.currentPageIndicatorTintColor = currentColor; } - (void)setOtherColor:(UIColor *)otherColor { _otherColor = otherColor; self.pageControl.pageIndicatorTintColor = otherColor; } #pragma mark - <UIScrollViewDelegate> - (void)scrollViewDidScroll:(UIScrollView *)scrollView { self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5); } @end
用到分页的新项目
// // ViewController.m // 02-用到分页的新项目 #import "ViewController.h" #import "XMGPageView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; XMGPageView *pageView = [XMGPageView pageView]; pageView.frame = CGRectMake(20, 50, 200, 140); pageView.frame = CGRectMake(20, 50, 200, 140); pageView.otherColor = [UIColor redColor]; pageView.imageNames = @[@"page_00", @"page_00", @"page_00", @"page_00", @"page_00"]; [self.view addSubview:pageView]; } @end
// // XMGPageView.h // 08-分页 #import <UIKit/UIKit.h> // 2.0 @interface XMGPageView : UIView + (instancetype)pageView; /** 图片名字 */ @property (nonatomic, strong) NSArray *imageNames; /** 其他圆点颜色 */ @property (nonatomic, strong) UIColor *otherColor NS_DEPRECATED_IOS(2_0, 2_0, "建议使用nbColor"); /** 当前圆点颜色 */ @property (nonatomic, strong) UIColor *currentColor; @end
#import "XMGPageView.h" @interface XMGPageView() <UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @property (weak, nonatomic) IBOutlet UIPageControl *pageControl; @end @implementation XMGPageView + (instancetype)pageView { return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject]; } - (void)layoutSubviews { [super layoutSubviews]; // 设置scrollView的frame self.scrollView.frame = self.bounds; // 获得scrollview的尺寸 CGFloat scrollW = self.scrollView.frame.size.width; CGFloat scrollH = self.scrollView.frame.size.height; // 设置pageControl CGFloat pageW = 100; CGFloat pageH = 20; CGFloat pageX = scrollW - pageW; CGFloat pageY = scrollH - pageH; self.pageControl.frame = CGRectMake(pageX, pageY, pageW, pageH); // 设置内容大小 self.scrollView.contentSize = CGSizeMake(self.imageNames.count * scrollW, 0); // 设置所有imageView的frame for (int i = 0; i<self.scrollView.subviews.count; i++) { UIImageView *imageView = self.scrollView.subviews[i]; imageView.frame = CGRectMake(i * scrollW, 0, scrollW, scrollH); } } #pragma mark - setter方法的重写 - (void)setImageNames:(NSArray *)imageNames { _imageNames = imageNames; // 移除之前的所有imageView // 让subviews数组中的所有对象都执行removeFromSuperview方法 [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; // 根据图片名创建对应个数的imageView for (int i = 0; i<imageNames.count; i++) { UIImageView *imageView = [[UIImageView alloc] init]; imageView.image = [UIImage imageNamed:imageNames[i]]; [self.scrollView addSubview:imageView]; } // 设置总页数 self.pageControl.numberOfPages = imageNames.count; // if (imageNames.count <= 1) { // self.pageControl.hidden = YES; // } else { // self.pageControl.hidden = NO; // } // self.pageControl.hidden = imageNames.count <= 1; // self.pageControl.hidesForSinglePage = YES; } - (void)setCurrentColor:(UIColor *)currentColor { _currentColor = currentColor; self.pageControl.currentPageIndicatorTintColor = currentColor; } - (void)setOtherColor:(UIColor *)otherColor { _otherColor = otherColor; self.pageControl.pageIndicatorTintColor = otherColor; } #pragma mark - <UIScrollViewDelegate> - (void)scrollViewDidScroll:(UIScrollView *)scrollView { self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5); } @end