#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#import "ViewController.h"
#import "MyScrollView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array = @[@"sc4.jpg",@"sc0.jpg",@"sc1.jpg",@"sc2.jpg",@"sc3.jpg",@"sc4.jpg",@"sc0.jpg"];
MyScrollView *myScrol = [[MyScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) andContentSize:CGSizeMake(WIDTH*7, HEIGHT) andHorizontal:NO andVertical:NO andBounces:NO andpagingEnabled:YES andWidth:WIDTH andHeight:HEIGHT andImageArray:array];
[self.view addSubview:myScrol];
[myScrol setPage:CGRectMake(40, HEIGHT-60, WIDTH-80, 30) andNumberOfPage:5 andCurrentPage:0 andCurrentColor:[UIColor yellowColor] andPageColor:[UIColor redColor]];
[self.view addSubview:myScrol.page];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface MyScrollView : UIScrollView<UIScrollViewDelegate>
{
NSTimer *_timer;
}
@property(nonatomic ,copy)NSArray *imageArray;//轮转图片名的数组
@property(nonatomic,strong)UIPageControl *page;//图片上的圆点
@property(nonatomic,assign)CGFloat width;//页面的宽
@property(nonatomic,assign)CGFloat height;//页面的高
-(id)initWithFrame:(CGRect)frame andContentSize:(CGSize)size andHorizontal:(BOOL)h andVertical:(BOOL)v andBounces:(BOOL)b andpagingEnabled:(BOOL)p andWidth:(CGFloat)width andHeight:(CGFloat)height andImageArray:(NSArray *)array;//初始化ScrollView
-(void)setPage:(CGRect)frame andNumberOfPage:(NSInteger) num andCurrentPage:(NSInteger)current andCurrentColor:(UIColor*)ccolor andPageColor:(UIColor *)pcolor;//初始化page
@end
#import "MyScrollView.h"
@implementation MyScrollView
-(id)initWithFrame:(CGRect)frame andContentSize:(CGSize)size andHorizontal:(BOOL)h andVertical:(BOOL)v andBounces:(BOOL)b andpagingEnabled:(BOOL)p andWidth:(CGFloat)width andHeight:(CGFloat)height andImageArray:(NSArray *)array{
if (self = [super initWithFrame:frame]) {//设置画布大小
//当width和height 同时小于画布大小的时候 ,控件无法滑动 ,有其中一个大于画布 则决定滑动的方向 同时大于则左右上下都可滑动
//设置画布中内容的大小
self.contentSize = size;
//横向滑动的条去掉
self.showsHorizontalScrollIndicator = h;
//上下的滑动的条去掉
self.showsVerticalScrollIndicator = v;
//反弹
self.bounces = b;
//翻页效果
self.pagingEnabled = p;
self.delegate = self;
_width = width;
_height = height;
_imageArray = array;
//设置timer
_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changeoffSet) userInfo:nil repeats:YES];
[self addImage];
}
return self;
}
-(void)addImage{
//将图片加入scrollView里面
for (int i=0; i<7; i++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(_width*i, 0, _width, _height)];
imageView.image = [UIImage imageNamed:_imageArray[i]];
[self addSubview:imageView];
}
}
//设置pageControl
-(void)setPage:(CGRect)frame andNumberOfPage:(NSInteger)num andCurrentPage:(NSInteger)current andCurrentColor:(UIColor *)ccolor andPageColor:(UIColor *)pcolor{
_page =[[UIPageControl alloc]initWithFrame:frame];
_page.numberOfPages = num;
_page.currentPage = current;
_page.currentPageIndicatorTintColor = ccolor;
_page.pageIndicatorTintColor = pcolor;
}
//timer要执行的方法 //自动翻转
-(void)changeoffSet{
[UIView animateWithDuration:1 animations:^{
self.contentOffset = CGPointMake(self.contentOffset.x+_width, 0);//contentOffSet 偏移量
}completion:^(BOOL finished) {
if (self.contentOffset.x == 6*_width) {
self.contentOffset = CGPointMake(_width, 0);
}
_page.currentPage = self.contentOffset.x/_width-1;
}];
}
//代理执行方法 //手动翻转
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//0 1 2 3 4 5 6
//4 0 1 2 3 4 0
if (scrollView.contentOffset.x == _width*6) {
scrollView.contentOffset = CGPointMake(_width, 0);
NSLog(@"1");
}
else if (scrollView.contentOffset.x ==0) {
scrollView.contentOffset = CGPointMake(_width*5, 0);
}
_page.currentPage = self.contentOffset.x/_width-1;
}
@end