滾動(dòng)視圖

#import "ViewController.h"

@interface ViewController ()<UIScrollerViewDelegate>

{

UIScrollView *scroll;? //滾動(dòng)視圖

NSArray *arr;? //數(shù)組

UIPageControl *p;? //分頁(yè)控件

NSInteger pagecount; //記錄頁(yè)數(shù)

NSTimer *timer; //計(jì)時(shí)器

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//獲取屏幕的寬

float width = self.view.frame.size.width;

//獲取屏幕的高

float height = self.view.frame.size.height;

//創(chuàng)建滾動(dòng)視圖

scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, width, height)];

float x = 0.0;

for (int i = 0 ; i < 4; i++ )

{

//創(chuàng)建圖片框

UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, width, height)];

//創(chuàng)建圖片數(shù)組

arr = @[[UIImage imageNamed:@"1.jpg"],[UIImage imageNamed:@"2.jpg"],[UIImage imageNamed:@"3.jpg"],[UIImage imageNamed:@"4.jpg"]];

//將圖片添加到圖片框上

imgView.image = arr[i];

//將圖片框添加到滾動(dòng)視圖上

[scroll addSubview:imgView];

//疊加

x += width;

}

//設(shè)置滾動(dòng)視圖的大小

scroll.contentSize = CGSizeMake(width * 4, height);

//去掉彈簧效果

scroll.bounces = NO;

//隱藏滾動(dòng)條

scroll.showsHorizontalScrollIndicator = NO;

//設(shè)置按頁(yè)滾動(dòng)

scroll.pagingEnabled = YES;

scroll.delegate = self;

//將滾動(dòng)視圖添加到視圖上

[self.view addSubview:scroll];

//創(chuàng)建分頁(yè)控件

p = [[UIPageControl alloc]initWithFrame:CGRectMake((width - 50)/2, 680, 100, 30)];

//設(shè)置頁(yè)碼個(gè)數(shù)

p.numberOfPages = 4;

//設(shè)置頁(yè)碼初始頁(yè)

p.currentPage = 0;

//設(shè)置頁(yè)碼顏色

p.pageIndicatorTintColor = [UIColor redColor];

//設(shè)置當(dāng)前頁(yè)碼的顏色

p.currentPageIndicatorTintColor = [UIColor cyanColor];

//將分頁(yè)控件添加到視圖上

[self.view addSubview:p];

//記錄當(dāng)前頁(yè)面的圖片頁(yè)數(shù)

pagecount = p.currentPage;

//創(chuàng)建定時(shí)器

timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(scroll) userInfo:nil repeats:YES];

}

//滾動(dòng)方法

-(void)scroll

{

pagecount++;

if (pagecount >= arr.count)

{

pagecount = 0;

}

//設(shè)置滾動(dòng)視圖的偏移量

[scroll setContentOffset:CGPointMake(pagecount * scroll.frame.size.width, 0) animated:YES];

}

//在滾動(dòng)的時(shí)候調(diào)用此方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

//當(dāng)前的偏移量

CGPoint point = scrollView.contentOffset;

//獲取當(dāng)前所在的位置

p.currentPage = point.x / scrollView.frame.size.width;

//創(chuàng)建立即體驗(yàn)按鈕

UIButton *btn = [[UIButton alloc]init];

//判斷滾動(dòng)到第四頁(yè)的時(shí)候 停止計(jì)時(shí)器 顯示立即體檢按鈕

if (p.currentPage == 3)

{

//停止定時(shí)器

[timer invalidate];

btn.frame = CGRectMake(110, 600, 200, 40);

//設(shè)置按鈕背景顏色

btn.backgroundColor = [UIColor clearColor];

//設(shè)置邊框

btn.layer.borderWidth = 1;

btn.layer.backgroundColor = [UIColor greenColor].CGColor;

[btn setTitle:@"立即體驗(yàn)" forState:UIControlStateNormal];

//設(shè)置文字顏色

[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

//設(shè)置圓角

btn.layer.cornerRadius = 8;

btn.layer.masksToBounds = YES;

//給按鈕添加點(diǎn)擊事件

[btn addTarget:self action:@selector(anniu) forControlEvents:UIControlEventTouchUpInside];

//將按鈕添加到視圖上

[self.view addSubview:btn];

//設(shè)置禁止用戶交互

scrollView.userInteractionEnabled = NO;

}

}

-(void)anniu

{

NSLog(@"走起");

}

//隱藏狀態(tài)欄

-(BOOL)prefersStatusBarHidden

{

return YES;

}

@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容