主要分为两步:1. 创建UIScrollView 2.创建UIPageControl。最后别忘了实现UIScrollViewDelegate
- (void)setupScrollView
{
//创建UIScrollView
UIScrollView *scorllView = [[UIScrollView alloc] init];
scorllView.frame = self.view.bounds;
[self.view addSubview:scorllView];
scorllView.delegate = self;
//添加图片
CGFloat width = scorllView.width;
CGFloat height = scorllView.height;
for (int i = 0; i < 4; ++i)
{
// 拼接图片名称
NSString *name = [NSString stringWithFormat:@"new_feature_%d", i+1];
UIImage *image = [UIImage imageWithName:name];
// 创建UIImageView
UIImageView *iv = [[UIImageView alloc] init];
iv.image = image;
// 设置frame
iv.width = width;
iv.height = height;
iv.y = 0;
iv.x = i * iv.width;
// 添加UIImageView到scrollerView
[scorllView addSubview:iv];
}
// 设置UISrollView的其他属性
// 设置contentsize
scorllView.contentSize = CGSizeMake(WZNewfeatureImageCount * width, 0);
// 设置分页
scorllView.pagingEnabled = YES;
// 隐藏指示条
scorllView.showsHorizontalScrollIndicator = NO;
// 设置没有弹簧效果
scorllView.bounces = NO;
}
- (void)setupPageControl
{
//创建UIPageControl
UIPageControl *control = [[UIPageControl alloc] init];
control.numberOfPages = WZNewfeatureImageCount;
//设置UIPageControl的frame
control.centerX = self.view.width * 0.5;
control.centerY = self.view.height - 30;
//设置当前页的颜色
control.currentPageIndicatorTintColor = [UIColor redColor];
//设置其他也的颜色
control.pageIndicatorTintColor = [UIColor greenColor];
//添加UIPageControl到控制器的View
[self.view addSubview:control];
self.control = control;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 1.计算页码
int page = scrollView.contentOffset.x / self.view.width;
// 2.设置页码
self.control.currentPage = page;
}
PS:模拟器上没 home键,怎么返回的
shift+cmd+h