#import "ViewController.h"
@interface ViewController ()
{
? ? UIScrollView*scroll;//滾動(dòng)視圖
? ? NSArray*imgArr;//圖片數(shù)組
? ? UIPageControl *page;//分頁(yè)控件
}
@end
@implementation ViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? //創(chuàng)建滾動(dòng)視圖
? ? scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];
? ? //設(shè)置代理
? ? scroll.delegate=self;
? ? //將滾動(dòng)視圖添加到視圖
? ? [self.view addSubview:scroll];
? ? //創(chuàng)建圖片數(shù)組
? ? imgArr = @[@"1",@"2",@"3",@"4"];
? ? //使用for循環(huán)添加圖片框? 設(shè)置圖片
? ? for(inti =0; i <4; 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= [UIImageimageNamed:imgArr[i]];
? ? ? ? //是否允許與用戶交互
? ? ? ? imgView.userInteractionEnabled = YES;
? ? ? ? //判斷最后一張圖片就顯示立即體驗(yàn)按鈕
? ? ? ? if(i ==3) {
? ? ? ? ? ? //創(chuàng)建立即體驗(yàn)按鈕
? ? ? ? ? ? UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
? ? ? ? ? ? //設(shè)置位置
? ? ? ? ? ? btn.frame=CGRectMake((self.view.frame.size.width-100)/2,600,100,44);
? ? ? ? ? ? //設(shè)置按鈕文字
? ? ? ? ? ? [btnsetTitle:@"立即體驗(yàn)"forState:UIControlStateNormal];
? ? ? ? ? ? //添加事件
? ? ? ? ? ? [btnaddTarget:self action:@selector(dicCilckBtn) forControlEvents:UIControlEventTouchUpInside];
? ? ? ? ? ? //設(shè)置圓角
? ? ? ? ? ? btn.layer.cornerRadius=8;
? ? ? ? ? ? //裁剪邊框
? ? ? ? ? ? btn.layer.masksToBounds=YES;
? ? ? ? ? ? //設(shè)置邊框
? ? ? ? ? ? btn.layer.borderWidth=1;
? ? ? ? ? ? btn.layer.borderColor = [UIColor cyanColor].CGColor;
? ? ? ? ? ? //將按鈕添加到圖片上
? ? ? ? ? ? [imgViewaddSubview:btn];
? ? ? ? }
? ? ? ? //將圖片框添加到滾動(dòng)視圖中
? ? ? ? [scrolladdSubview:imgView];
? ? }
? ? //設(shè)置滾動(dòng)范圍
? ? scroll.contentSize = CGSizeMake(self.view.frame.size.width *4, self.view.frame.size.height);
? ? //設(shè)置分頁(yè)滾動(dòng)
? ? 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)/2, 670,100, 30)];
? ? //設(shè)置分頁(yè)
? ? page.numberOfPages = 4;
? ? //設(shè)置當(dāng)前頁(yè)碼顏色
? ? page.currentPageIndicatorTintColor = [UIColor redColor];
? ? //設(shè)置分頁(yè)顏色
? ? page.pageIndicatorTintColor = [UIColor blackColor];
? ? //添加到視圖
? ? [self.view addSubview:page];
}
//滾動(dòng)視圖的協(xié)議方法 - - - -當(dāng)滾動(dòng)結(jié)束的時(shí)候調(diào)用
- (void)scrollViewDidScroll:(UIScrollView*)scrollView{
? ? //將滾動(dòng)的頁(yè)數(shù)和滾動(dòng)視圖關(guān)聯(lián)
? ? page.currentPage = scroll.contentOffset.x / self.view.frame.size.width;
}