//
// ViewController.m
// UCTestAutoScroll
//
// Created by heju on 2018/4/20
#define UC_TEST_SCROLLVIEW_WITH250
#define UC_TEST_SCROLLVIEW_HEIGHT60
#import "ViewController.h"
#define card_page_conrol_width70
#define card_page_control_height10
#define card_page_control_bottom_margin20
@interface ViewController ()
@property (nonatomic,strong)UIScrollView *scrollview;
@property (nonatomic,strong)UIPageControl *pageControl;
@property (nonatomic,strong)NSTimer *timer;
@property (nonatomic,strong)NSMutableArray *items;
@property (nonatomic, assign) int currentIndex;
@property (nonatomic,assign) CGSize viewSize;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor grayColor];
_currentIndex = 0;
[self setupSubView];
}
- (NSMutableArray *)items{
if(_items==nil) {
_items= [@[@"1",@"2",@"3"]mutableCopy];
}
return _items;
}
- (void)setupSubView {
_viewSize = self.view.frame.size;
_scrollview = [[UIScrollView alloc] initWithFrame: CGRectMake(0, 200, _viewSize.width,100)];
_scrollview.showsVerticalScrollIndicator = NO;
_scrollview.showsHorizontalScrollIndicator = NO;
_scrollview.delegate = self;
_scrollview.scrollEnabled = YES;
_scrollview.pagingEnabled = YES;
_scrollview.bounces = NO;
_scrollview.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_scrollview];
_pageControl= [[UIPageControlalloc]init];
_pageControl.frame = CGRectMake((_viewSize.width - card_page_conrol_width)/2 ,280,card_page_conrol_width,card_page_control_height);
_pageControl.numberOfPages = 3;
_pageControl.currentPageIndicatorTintColor =[UIColor purpleColor];
_pageControl.pageIndicatorTintColor = [UIColor yellowColor] ;
_pageControl.currentPage = 0;
[_pageControl addTarget:self action:@selector(changePage:)forControlEvents:UIControlEventValueChanged];
_pageControl.backgroundColor = [UIColor grayColor];
[self.view addSubview:_pageControl];
_scrollview.contentSize = CGSizeMake(_viewSize.width * [self.items count], _viewSize.height);
for(NSIntegeri =0; i <= [self.itemscount]; i++)
{
UIImageView*itemImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(i*_viewSize.width,0,_viewSize.width,100)];
if(i ==3) {
[itemImageViewsetImage:[selfimageWithName:self.items[0]andType:@"jpg"]];
}else{
[itemImageViewsetImage:[selfimageWithName:self.items[i]andType:@"jpg"]];
}
itemImageView.userInteractionEnabled=YES;
[self.scrollviewaddSubview:itemImageView];
}
/*自动轮播*/
[self setupTimer];
}
- (void)setupTimer {
[self invalidateTimer]; // 创建定时器前先停止定时器,不然会出现僵尸定时器,导致轮播频率错误
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(automaticScroll) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)automaticScroll {
if (self.currentIndex >= self.items.count) {
self.currentIndex=1;
}else{
self.currentIndex+=1;
}
[self.scrollview setContentOffset:CGPointMake(_viewSize.width * self.currentIndex, 0) animated:YES];
}
- (void)invalidateTimer
{
if(self.timer!=nil) {
[self.timerinvalidate];
self.timer=nil;
}
}
#pragma mark UIScrollViewDelegate Method
- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
[self invalidateTimer];
}
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
intpage = scrollView.contentOffset.x/ scrollView.frame.size.width;
_pageControl.currentPage= page;
}
- (void)scrollViewDidEndDragging:(UIScrollView*)scrollView willDecelerate:(BOOL)decelerate
{
[self setupTimer];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView*)scrollView {
if (self.currentIndex >= self.items.count) {
[scrollViewsetContentOffset:CGPointMake(0, 0) animated:NO];
}
}
- (void)changePage:(UIPageControl*)sender {
intpage = (int)sender.currentPage;
[_scrollview setContentOffset:CGPointMake(page *self.viewSize.width , 0) animated:YES];
}
- (UIImage*)imageWithName:(NSString*)name andType:(NSString*)type {
if(name.length>0&& type.length>0) {
NSString*imagePath = [[NSBundlemainBundle]pathForResource:nameofType:type];
UIImage*image = [UIImageimageWithContentsOfFile:imagePath];
returnimage;
}
return nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
网友评论