HomePageViewController
2015-07-05 12:11
369 查看
– (void)createScrollView
{
}
{
int page = (int)_pageControl.currentPage;
}
{
CGFloat scrollViewW = scrollView.frame.size.width;
CGFloat x = scrollView.contentOffset.x;
int page = (x +scrollViewW/2) / scrollViewW;
_pageControl.currentPage = page;
}
{
[self removeTimer];
}
{
[_timer invalidate];
}
–(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self addTimer];
}
{
_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
}
{
int page = (int)_pageControl.currentPage;
if (page == _imageArray.count -1) {
page = 0;
}
else
{
page ++;
}
}
{
_imageArray = @[@"img_head1",@"img_head2",@"img_head3"]; _circulationView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 130)]; _circulationView.frame = [ToolViewAndData MyAutoLayout:_circulationView]; _circulationView.delegate = self; _circulationView.showsHorizontalScrollIndicator = NO; [self.view addSubview:_circulationView]; CGFloat imageW = _circulationView.frame.size.width; CGFloat imageH = _circulationView.frame.size.height; _circulationView.contentSize = CGSizeMake(imageW *_imageArray.count, 0); for (int i =0; i<_imageArray.count; i++) { UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i *imageW, 0, imageW, imageH)]; imageView.image = [UIImage imageNamed:_imageArray[i]]; [_circulationView addSubview:imageView]; } _circulationView.pagingEnabled = YES; _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(120, 100, 80, 30)]; _pageControl.frame = [ToolViewAndData MyAutoLayout:_pageControl]; _pageControl.currentPage = 0; _pageControl.numberOfPages = _imageArray.count; [self.view addSubview:_pageControl]; [_pageControl addTarget:self action:@selector(changePageClick:) forControlEvents:UIControlEventValueChanged]; [self addTimer];
}
pragma mark ——-点击pagecontroll
–(void)changePageClick:(UIPageControl *)sender{
int page = (int)_pageControl.currentPage;
CGFloat x = page * _circulationView.frame.size.width; _circulationView.contentOffset = CGPointMake(x, 0);
}
pragma mark —– ScrollView滚动的时候调用
– (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat scrollViewW = scrollView.frame.size.width;
CGFloat x = scrollView.contentOffset.x;
int page = (x +scrollViewW/2) / scrollViewW;
_pageControl.currentPage = page;
}
pragma mark —— 开始拖拽的时候调用
–(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self removeTimer];
}
pragma mark ——- 关闭定时器
– (void)removeTimer{
[_timer invalidate];
}
–(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self addTimer];
}
pragma mark —-开启定时器
– (void)addTimer{
_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
}
pragma mark —— 定时器滚动图片
–(void)nextImage{
int page = (int)_pageControl.currentPage;
if (page == _imageArray.count -1) {
page = 0;
}
else
{
page ++;
}
CGFloat x = page * _circulationView.frame.size.width; _circulationView.contentOffset = CGPointMake(x, 0);
}