ios 学习 广告图片轮播器

时间:2023-12-28 13:07:20
 //
// ViewController.m
// 图片轮播器
//
// Created by zjj on 15/5/23.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import "ViewController.h"
#define kCount 5
@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic,strong)UIScrollView *scroView;
@property (nonatomic,strong)UIPageControl *pageControl;
@property (nonatomic,strong)NSTimer *time;
@end @implementation ViewController
/**
开发思路
1>创建scrollView
2>用UIimageView加载图片 并修改图片的x值
3>创建分页控件UIpageControl
4>绑定分页圆点点 点击圆点点换下一页
5>绑定时钟
*/
#pragma mark 初始化scrollView
- (UIScrollView *)scroView
{
if (_scroView == nil) {
_scroView = [[UIScrollView alloc]initWithFrame:CGRectMake(, , , )];
_scroView.backgroundColor = [UIColor blueColor];
[self.view addSubview:_scroView];
_scroView.bounces = NO;
_scroView.showsHorizontalScrollIndicator = NO;
_scroView.showsVerticalScrollIndicator = NO;
_scroView.contentSize = CGSizeMake(_scroView.bounds.size.width * kCount, );
self.scroView.pagingEnabled = YES;
_scroView.delegate = self;
}
return _scroView ;
}
#pragma mark 初始化PageCOntrol
- (UIPageControl *)pageControl
{
if (_pageControl == nil) {
// 分页控件 跟scrollView 是独立的
_pageControl = [[UIPageControl alloc]init];
_pageControl.numberOfPages = kCount;
CGSize size = [_pageControl sizeForNumberOfPages:kCount];
_pageControl.bounds = CGRectMake(, , size.width, size.height);
_pageControl.center = CGPointMake(self.scroView.center.x, self.scroView.center.y+(self.scroView.center.y/));
_pageControl.pageIndicatorTintColor = [UIColor redColor];
_pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
[self.view addSubview:_pageControl];
self.pageControl = _pageControl;
}
return _pageControl;
} - (void)viewDidLoad {
[super viewDidLoad];
for (int i = ; i < kCount; i++) {
NSString *icon = [NSString stringWithFormat:@"img_%02d",i + ];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:self.scroView.bounds];
imgView.image = [UIImage imageNamed:icon];
[self.scroView addSubview:imgView];
}
// 添加事件之后执行该块代码重新赋值x值
[self.scroView.subviews enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) {
CGRect frame = imageView.frame;
frame.origin.x = idx * frame.size.width;
imageView.frame = frame;
}];
self.pageControl.currentPage = ;
[self.pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventTouchUpInside];
[self start];
}
#pragma mark 启动时钟
- (void)start
{
// self.time = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];// 按住屏幕 画面会停止轮播但 时钟会累积 连跳好几页
self.time = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];// 按住屏幕时钟 画面仍然不会停止轮播
[[NSRunLoop currentRunLoop] addTimer:self.time forMode:NSRunLoopCommonModes];// 添加到(运行循环)轮循
} - (void)changeTime
{
int page = (self.pageControl.currentPage + ) % kCount;
self.pageControl.currentPage = page;
[self pageChange:self.pageControl];
} #pragma mark 对scrollView进行操作 根据图片偏移位置计算页数
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// NSLog(@"%@,%f", NSStringFromCGPoint(_scroView.contentOffset),_scroView.bounds.size.width);//取出scrollview中当前页的x
int page = _scroView.contentOffset.x / _scroView.bounds.size.width;
self.pageControl.currentPage = page; //当前页数
} - (void)pageChange :(UIPageControl *)page
{
CGFloat x = page.currentPage * self.scroView.bounds.size.width;
[self.scroView setContentOffset:CGPointMake(x, )animated:YES];
}
#pragma mark 拖动图片时停止时钟
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.time invalidate];
}
#pragma mark 松手后再次启动时钟
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
[self start];
}
@end