#import "ViewController.h"
#import "SecViewController.h"
@interface ViewController ()<UIScrollviewDelegate>
{
? ? UIScrollView *scroll;? //滾動(dòng)視圖
? ? NSArray *imgArr; //圖片數(shù)組
? ? UIPageControl *page; //分頁(yè)控件
? ? NSTimer *theTime;
? ? NSInteger thePageTime;
}
@end
@implementation ViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? //設(shè)置背景顏色
? ? self.view.backgroundColor = [UIColor cyanColor];
? ? //創(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.jpg",@"2.jpg",@"3.jpg",@"4.jpg"];
? ? //使用for循環(huán)設(shè)置圖片
? ? for (int i=0; i<4; i++) {
? ? ? ? //創(chuàng)建圖片框
? ? ? ? UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height)];
? ? ? ? //設(shè)置圖片
? ? ? ? image.image = [UIImage imageNamed:imgArr[i]];
? ? ? ? //允許與用戶交互
? ? ? ? image.userInteractionEnabled = YES;
? ? ? ? //將圖片添加到滾動(dòng)視圖
? ? ? ? [scroll addSubview:image];
? ? ? ? /**
? ? ? ? if (i == 3)
? ? ? ? {
? ? ? ? // 創(chuàng)建立即體驗(yàn)按鈕
? ? ? ? UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
? ? ? ? btn.frame = CGRectMake((self.view.frame.size.width - 100)/2, 600, 100, 44);
? ? ? ? [btn setTitle:@"立即體驗(yàn)" forState:UIControlStateNormal];
? ? ? ? //? ? ? ? ? ? btn.backgroundColor = [UIColor cyanColor];
? ? ? ? // 給按鈕添加點(diǎn)擊事件
? ? ? ? [btn addTarget:self action:@selector(didClickBtn) forControlEvents:UIControlEventTouchUpInside];
? ? ? ? // 設(shè)置圓角
? ? ? ? btn.layer.cornerRadius = 8;
? ? ? ? btn.layer.masksToBounds = YES;
? ? ? ? // 設(shè)置邊框
? ? ? ? btn.layer.borderWidth = 1;
? ? ? ? btn.layer.borderColor = [UIColor cyanColor].CGColor;
? ? ? ? // 將按鈕添加到圖片上
? ? ? ? [image addSubview:btn];
? ? ? ? }
? ? ? ? */
? ? }
? ? //設(shè)置滾動(dòng)范圍
? ? scroll.contentSize = CGSizeMake(self.view.frame.size.width*4, self.view.frame.size.height);
? ? //取消彈簧效果
? ? scroll.bounces = NO;
? ? //設(shè)置分頁(yè)效果
? ? scroll.pagingEnabled = YES;
? ? //隱藏水平滾動(dòng)條
? ? scroll.showsHorizontalScrollIndicator = NO;
? ? //創(chuàng)建分頁(yè)控件
? ? page = [[UIPageControl alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100)/2, self.view.frame.size.height-100, 100, 30)];
? ? //設(shè)置頁(yè)數(shù)
? ? page.numberOfPages = 4;
? ? //設(shè)置頁(yè)碼顏色
? ? page.pageIndicatorTintColor = [UIColor redColor];
? ? //設(shè)置當(dāng)前頁(yè)碼顏色
? ? page.currentPageIndicatorTintColor = [UIColor blackColor];
? ? //添加到視圖
? ? [self.view addSubview:page];
? ? //記錄當(dāng)前的圖片
? ? thePageTime = page.currentPage;
? ? //創(chuàng)建定時(shí)器
? ? theTime = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(Scroll) userInfo:nil repeats:YES];
}
//滾動(dòng)視圖協(xié)議的方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
? ? //將滾動(dòng)的頁(yè)數(shù)和滾動(dòng)視圖關(guān)聯(lián)
? ? page.currentPage = scroll.contentOffset.x/self.view.frame.size.width;
}
//定時(shí)器方法
-(void)Scroll{
? ? thePageTime++;
? ? if (thePageTime >= imgArr.count) {
? ? ? ? thePageTime = 3;
? ? ? ? [theTime invalidate];
? ? ? ? theTime = nil;
? ? ? ? [self presentViewController:[SecViewController new] animated:YES completion:nil];
? ? }
? ? //計(jì)算滾動(dòng)視圖的偏移量
? ? [scroll setContentOffset:CGPointMake(thePageTime * scroll.frame.size.width, 0) animated:YES];
}
-(void)didClickBtn{
}
@end