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