效果图
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic, assign) int pageSum; //分页总数
@property (weak, nonatomic) UIImage *image;
@property (strong, nonatomic) UIImageView *imageView;
@property (nonatomic, assign) CGFloat imgW; //图片的宽度
@property (nonatomic, assign) CGFloat imgH; //图片的高度
@property (nonatomic, weak) UIPageControl *pageControl;//分页控制器
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.pageSum = 4; //总页数
self.imgW = self.scrollView.frame.size.width;//375;//
self.imgH = self.scrollView.frame.size.height;//667;
NSLog(@"W = %f,H = %f",self.imgW,self.imgH);
//显示多张图片
for (int i = 0; i< self.pageSum; i++) {
self.image = [UIImage imageNamed:[NSString stringWithFormat:@"guide%d",i+1]];
self.imageView = [[UIImageView alloc]initWithImage:self.image];
self.imageView.frame = CGRectMake(i*self.imgW, 0, self.imgW, self.imgH);
[self.scrollView addSubview: self.imageView];
//最后一张的时候,创建按钮
if(i<self.pageSum-1){
[self skipPageWithView:self.imageView];
}else if(i == self.pageSum -1){
[self enterPageWithView];
}
}
//设置内容的大小
self.scrollView.contentSize = CGSizeMake(self.imgW * self.pageSum, 0);
//分页指示器
UIPageControl *pageControl = [[UIPageControl alloc]init];
pageControl.numberOfPages = self.pageSum;
pageControl.currentPage = 0;//现在所在的页数
pageControl.frame = CGRectMake(0, self.imgH*0.72, self.imgW, 28);
pageControl.pageIndicatorTintColor = [UIColor blackColor];
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
[self.view addSubview:self.scrollView];
[self.view addSubview:pageControl];
self.pageControl = pageControl;
self.scrollView.showsHorizontalScrollIndicator = NO;//不显示水平滚动条
//scrollView自带的动画
//[self.scrollView setContentOffset:CGPointMake(self.imgW, 0) animated:YES];
self.scrollView.bounces = YES; //scrollView弹簧效果
self.scrollView.pagingEnabled = YES;// 启动分页
//设置代理对象(当前控制器为代理对象)
self.scrollView.delegate = self;
}
//跳过页面与视图
-(void)skipPageWithView:(UIImageView *)imageView{
imageView.userInteractionEnabled = true;// 允许和用户交互
UIButton *skipBtn = [[UIButton alloc]init];
//设置字体
[skipBtn setBackgroundColor:[UIColor greenColor]];//greenColor
[skipBtn setTitle:@"跳过" forState:UIControlStateNormal];
[skipBtn setTitle:@"飞过" forState:UIControlStateHighlighted];
[skipBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[skipBtn setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
[skipBtn sizeToFit];
CGFloat skipBtnY = imageView.frame.size.height - 50;
skipBtn.frame = CGRectMake(self.imgW*0.5-75, skipBtnY, 150, 35);
[skipBtn addTarget:self action:@selector(didSkipBtnClick) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:skipBtn];
}
//跳过按钮点击事件
-(void)didSkipBtnClick{
NSLog(@"跳过按钮点击事件...");
}
//进入页面与视图
-(void)enterPageWithView{//:(UIImageView*)imageView{
self.imageView.userInteractionEnabled = true;// 允许和用户交互
UIButton *enterBtn = [[UIButton alloc]init];
UIImage * enterImg = [UIImage imageNamed:@"EnterButtonBg"];
[enterBtn setImage:enterImg forState:UIControlStateNormal];
CGFloat enterBtnY = self.imageView.frame.size.height - 50;
enterBtn.frame = CGRectMake(0, enterBtnY, self.imgW, 35);//设置frame
[enterBtn addTarget:self action:@selector(didEnterBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self.imageView addSubview:enterBtn];
}
-(void)didEnterBtnClick{
NSLog(@"进入按钮点击事件...");
}
//监听“滚动”
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//获取偏移量,计算出当前的页数(偏移量/每页的宽度).加0.5是因为,如果在两页之间,则pageControl控件会动!
double page = scrollView.contentOffset.x/scrollView.frame.size.width;
self.pageControl.currentPage = (int)(page+0.5);
}
@end