首先定義幾個全局屬性
{
UIScrollView *scroll;//滾動視圖
NSArray *imgarr;//圖片數(shù)組
UIPageControl *page;//分頁
}
在viewDidLoad里:
//創(chuàng)建滾動視圖
scroll = [[UIScrollView alloc]initWithFrame:self.view.frame];
//代理
scroll.delegate = self;
//添加到視圖
[self.view addSubview:scroll];
//圖片數(shù)組
imgarr = @[@"1",@"2",@"3",@"4"];//添加圖片
//for循環(huán)添加圖片
for (int i = 0; i < imgarr.count; i++)
{
//創(chuàng)建圖片框
UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width *i, 0, self.view.frame.size.width, self.view.frame.size.height)];
//設置圖片
imgview.image = [UIImage imageNamed:imgarr[i]];
//是否與用戶交互
imgview.userInteractionEnabled = YES;
//判斷,最后一張圖片就添加按鈕
if (i == 3)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//設置按鈕位置
btn.frame = CGRectMake((self.view.frame.size.width - 100)/2, 600, 100, 44);
//按鈕文字
[btn setTitle:@"立即體驗" forState:UIControlStateNormal];
//注冊監(jiān)聽事件
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchDown];
//設置圓角
btn.layer.cornerRadius = 8;
//裁剪
btn.layer.masksToBounds = YES;
//背景顏色
btn.backgroundColor = [UIColor blueColor];
//添加到視圖
[imgview addSubview:btn];
}
//添加到滾動視圖
[scroll addSubview:imgview];
}
//設置吧滾動范圍
scroll.contentSize = CGSizeMake(self.view.frame.size.width *imgarr.count, self.view.frame.size.height);
//設置分頁效果
scroll.pagingEnabled = YES;
//去除彈窗效果
scroll.bounces = NO;
//隱藏水平滾動條
scroll.showsHorizontalScrollIndicator = NO;
//創(chuàng)建分頁控件
page = [[UIPageControl alloc]initWithFrame:CGRectMake((self.view.frame.size.width)-100, 650, 100, 30)];
//設置分頁個數(shù)
page.numberOfPages = imgarr.count;
//隱藏tabbar和navigation
self.navigationController.navigationBar.hidden = YES;
self.tabBarController.tabBar.hidden = YES;
在下面點擊事件:
-(void)click{
//取消隱藏tabbar和navigation
self.navigationController.navigationBar.hidden = NO;
self.tabBarController.tabBar.hidden = NO;
//在里面寫內(nèi)容
}