定時器+分頁
#import <UIKit/UIKit.h>
@interface XMGPageView : UIView
+ (instancetype)pageView;
/** 圖片名字 */
@property (nonatomic, strong) NSArray *imageNames;
/** 其他圓點(diǎn)顏色 */
@property (nonatomic, strong) UIColor *otherColor;
/** 當(dāng)前圓點(diǎn)顏色 */
@property (nonatomic, strong) UIColor *currentColor;
@end
#import "XMGPageView.h"
@interface XMGPageView() <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
/** 定時器 */
@property(nonatomic,strong)NSTimer * timer ;
@end
@implementation XMGPageView
/**
* 當(dāng)控件通過代碼創(chuàng)建時新思,就會調(diào)用這個方法
* 當(dāng)控件通過代碼創(chuàng)建時纲熏,想做一些初始化操作贺待。應(yīng)該在這個方法中執(zhí)行
*/
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setup];
// 添加子控件代碼
}
return self;
}
/**
* 當(dāng)控件從xib\storyboard中創(chuàng)建完畢時,就會調(diào)用這個方法
* 當(dāng)控件從xib\storyboard中創(chuàng)建完畢后的初始化操作趁耗。應(yīng)該在這個方法中執(zhí)行
*/
- (void)awakeFromNib
{
[self setup];
}
/**
* 初始化代碼
*/
- (void)setup
{
self.scrollView.backgroundColor = [UIColor redColor];
}
/**
* 當(dāng)控件是通過xib\storyboard創(chuàng)建時,會調(diào)用這個方法來初始化控件
*/
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
}
return self;
}
+ (instancetype)pageView
{
return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}
/**
* 當(dāng)控件的尺寸發(fā)生改變的時候玛荞,會自動調(diào)用這個方法
*/
- (void)layoutSubviews
{
[super layoutSubviews];
// 設(shè)置scrollView的frame
self.scrollView.frame = self.bounds;
// 獲得scrollview的尺寸
CGFloat scrollW = self.scrollView.frame.size.width;
CGFloat scrollH = self.scrollView.frame.size.height;
// 設(shè)置pageControl
CGFloat pageW = 100;
CGFloat pageH = 20;
CGFloat pageX = scrollW - pageW;
CGFloat pageY = scrollH - pageH;
self.pageControl.frame = CGRectMake(pageX, pageY, pageW, pageH);
// 設(shè)置內(nèi)容大小
self.scrollView.contentSize = CGSizeMake(self.imageNames.count * scrollW, 0);
// 設(shè)置所有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數(shù)組中的所有對象都執(zhí)行removeFromSuperview方法
[self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
// 根據(jù)圖片名創(chuàng)建對應(yīng)個數(shù)的imageView
for (int i = 0; i<imageNames.count; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:imageNames[i]];
[self.scrollView addSubview:imageView];
}
// 設(shè)置總頁數(shù)
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);
}
#pragma mark -定時器設(shè)置
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self stopTimer];
}
-(void)stopTimer
{
[self.timer invalidate];
self.timer=nil;
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self startTimer];
}
-(void)startTimer
{
self.timer=[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}
-(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];
NSLog(@"---------nextPage");
}
@end
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];
[self.view addSubview:pageView];
self.pageView = pageView;
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者