主要用Xib方式,代码后期还会有进一步的优化
下次用代码的时候一定要复制一份再用,直接拿出源代码来不小心哪里碰了下,耽误了10多分钟去找bug,郁闷!!!
功能:类似于手机打开新浪网首页最上面的大图片,自动滚动,右下角有个页数显示。
思路和注意点:1、主要用到的是scrollView和page control
2、在Xib中布置好控件
3、封装三部曲(首先在自定义构造方法内添加子控件,其次在layoutSubview中添加子控件的尺寸(此方法有随着外部尺寸变化,子控件尺寸比例随
着变化的动能),最后重写setter方法,拿到模型数据)
4、其它细节的设置如:翻页动画,page control小点的颜色变化,单页时候page control自动隐藏等等
使用的主要方法:1、加载Xib的方法:loadNibNamed
2、封装子控件的三步:- (instancetype)initWithFrame:(CGRect)frame{}
- (void)layoutSubviews{}
- (void)setImageNames:(NSArray *)imageNames{}
代码实现:
ZWpageView.h中:
1 #import <UIKit/UIKit.h> 2 @interface ZWpageView : UIView <UIScrollViewDelegate> 3 4 + (instancetype)pageView; 5 /** 图片数据 */ 6 @property (strong, nonatomic)NSArray *imageNames; 7 /** 其它圆点颜色 */ 8 @property (strong, nonatomic)UIColor *otherColor; 9 /** 当前圆点颜色 */ 10 @property (strong, nonatomic)UIColor *currentColor; 11 12 @end
ZWpageView.m中
#import "ZWpageView.h" @interface ZWpageView() @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @property (weak, nonatomic) IBOutlet UIPageControl *pageControl; /** 定时器 */ @property (strong, nonatomic)NSTimer *timer; @end @implementation ZWpageView #pragma mark - 初始化方法 + (instancetype)pageView{ return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject]; } // 当xib\storyboard创建时,会调用这个方法来初始化控件 - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self setup]; } return self; } /** * 当xib\storyboard创建完毕时,会调用这个方法来初始化控件 * 当xib\storyboard创建完毕后的初始化操作,应该在这里面进行 */ - (void)awakeFromNib { [self setup]; } - (void)setup { self.scrollView.backgroundColor = [UIColor blueColor]; //开启定时器 [self startTimer]; } - (void)layoutSubviews{ [super layoutSubviews]; //设置scrollView的frame self.scrollView.frame = self.bounds; CGFloat imageW = self.scrollView.frame.size.width; CGFloat imageH = self.scrollView.frame.size.height; //设置pageControl CGFloat pageW = 100; CGFloat pageH = 37; self.pageControl.frame = CGRectMake(imageW - pageW, imageH - pageH, pageW, pageH); //单页时候pageControl自动隐藏 // self.pageControl.hidesForSinglePage = YES; //xib中可以直接勾上!!! self.scrollView.contentSize = CGSizeMake(self.imageNames.count * imageW, 0); for (int i=0; i<self.scrollView.subviews.count; i++) { UIImageView *imageView = self.scrollView.subviews[i]; imageView.frame = CGRectMake(i * imageW, 0, imageW,imageH); } } #pragma mark - 代理 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ self.pageControl.currentPage = (int)(self.scrollView.contentOffset.x / self.scrollView.frame.size.width + 0.5); } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [self stopTimer]; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { [self startTimer]; } #pragma mark - 定时器控制 - (void)startTimer { //创建定时器 // NSTimer *timer = [NSTimer timerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES]; // [timer fire]; //这个方法不行,执行一次后就释放,不会保存 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.1 target:self selector:@selector(nextPage) userInfo:nil repeats:YES]; } - (void)stopTimer { [self.timer invalidate]; self.timer = nil; } - (void)nextPage { NSInteger page = self.pageControl.currentPage + 1; if (page == self.pageControl.numberOfPages) { page = 0; } CGPoint offset = self.scrollView.contentOffset; offset.x = page * self.scrollView.frame.size.width; [self.scrollView setContentOffset:offset animated:YES];//跳转到你所指定内容的坐标,YES表示跳转有动画 } #pragma mark - set方法重写 - (void)setImageNames:(NSArray *)imageNames{ _imageNames = imageNames; //由于可能用到多次set方法,所以重写set方法时候应该删除点之前的set方法内的内容(不要for循环删子控件) [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; //取出图片放入到scrollView中 for (int i = 0; i<imageNames.count; i++) { UIImageView *imageView = [[UIImageView alloc] init]; imageView.image = [UIImage imageNamed:imageNames[i]]; [self.scrollView addSubview:imageView]; } //小的细节设置 self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.pagingEnabled = YES; self.pageControl.numberOfPages = imageNames.count; } - (void)setOtherColor:(UIColor *)otherColor{ _otherColor = otherColor; self.pageControl.pageIndicatorTintColor = otherColor; } - (void)setCurrentColor:(UIColor *)currentColor{ _currentColor = currentColor; self.pageControl.currentPageIndicatorTintColor = currentColor; } @end
学习自:小码哥
时间: 2024-10-09 10:02:22