需求:
- 啟動(dòng)程序后,有個(gè)介紹app新特性的界面
思路:
- 新特性界面,其實(shí)就多個(gè)圖片組合在一起,展示出來而已!
- 使用UIScrollView,實(shí)現(xiàn)滾動(dòng)動(dòng)畫
- 新特性視圖,有個(gè)分頁功能,方便直觀的展現(xiàn)用戶內(nèi)容!(UIPageControl)
- UIPageControl和UIScrollView實(shí)現(xiàn)聯(lián)動(dòng)!(遵循代理,成為代理類,實(shí)現(xiàn)滑動(dòng)改變分頁控件方法)
- 在最后一頁,顯示"進(jìn)入微博"按鈕,注意它是背景圖片按鈕!currentBackgroundImage(當(dāng)前按鈕的背景圖片) setBackgroundImage (設(shè)置背景圖片)
#import "WBNewFeatureViewController.h"
#import "UIButton+Extension.h"
@interface WBNewFeatureViewController ()<UIScrollViewDelegate>
@property (nonatomic,weak)UIPageControl *pageControl;
@end
@implementation WBNewFeatureViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//創(chuàng)建scrollView
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame: self.view.bounds];
//開啟分頁
scrollView.pagingEnabled = YES;
//去除水平方向滾動(dòng)條
scrollView.showsHorizontalScrollIndicator = NO;
//監(jiān)聽滑動(dòng)-->成為代理
scrollView.delegate = self;
//動(dòng)畫圖片個(gè)數(shù)
NSInteger count= 4;
for (int i=0; i<count; i++) {
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"new_feature_%d",i+1]];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
//設(shè)置imageView的大小
imgView.size = scrollView.size;
imgView.x = i*scrollView.width;
[scrollView addSubview:imgView];
if (i == count-1) {
//最后一頁
[self setupLastPageWithView:imgView];
}
}
//設(shè)置scrollView的內(nèi)容大小
[scrollView setContentSize:CGSizeMake(count * scrollView.width, 0)];
[self.view addSubview:scrollView];
/*
添加分頁控件
1. 創(chuàng)建分頁控件
1. 分頁控件的位置
2. 分頁控件選中與默認(rèn)顏色(如沒有,則顯示為白色)
2. 分頁控件和UIScrollView關(guān)聯(lián)
1. 遵循代理
2. 設(shè)置當(dāng)前為代理類(滑動(dòng)時(shí),分頁控件改變)
3. 實(shí)現(xiàn)代理方法(滑動(dòng)時(shí),改變分頁控件)
*/
UIPageControl * pageControl = [[UIPageControl alloc]init];
//設(shè)置顯示幾頁
pageControl.numberOfPages = count;
//設(shè)置選中的顏色與默認(rèn)的顏色
pageControl.pageIndicatorTintColor = [UIColor grayColor];
pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
//設(shè)置位置
pageControl.centerX = self.view.width*0.5;
pageControl.y = self.view.height - 100;
self.pageControl = pageControl;
[self.view addSubview:pageControl];
}
//設(shè)置最后頁視圖
- (void)setupLastPageWithView:(UIImageView *)imageView{
imageView.userInteractionEnabled = YES;
//'進(jìn)入微博'按鈕(背景圖片按鈕)
UIButton *enterButton = [[UIButton alloc]init];
//正常狀態(tài)圖片按鈕
[enterButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"]forState:UIControlStateNormal];
//高亮狀態(tài)圖片按鈕
[enterButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
//設(shè)置按鈕大小(currentBackgroundImage - 背景圖片)
enterButton.size = enterButton.currentBackgroundImage.size;
//設(shè)置按鈕位置
enterButton.centerX = imageView.width * 0.5;
enterButton.y = imageView.height - 150;
//設(shè)置標(biāo)題
[enterButton setTitle:@"進(jìn)入微博" forState:UIControlStateNormal];
//成為imageView的子類
[imageView addSubview:enterButton];
//分享按鈕
UIButton *shareBtn = [[UIButton alloc] init];
[shareBtn setTitle:@"分享到微博" forState:UIControlStateNormal];
//設(shè)置不同狀態(tài)的圖片
[shareBtn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
[shareBtn setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
[shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[shareBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//自適應(yīng)內(nèi)容大小
[shareBtn sizeToFit];
shareBtn.centerX = enterButton.centerX;
shareBtn.y = enterButton.y - 40;
[imageView addSubview:shareBtn];
}
//"進(jìn)入微博"按鈕點(diǎn)擊事件
-(void)shareBtnClick:(UIButton*)button{
button.selected = !button.selected;
NSLog(@"%s",__func__);
}
// 已經(jīng)滑動(dòng)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//contentOffset 偏移量
double page = scrollView.contentOffset.x /scrollView.width;
//指定當(dāng)前頁所展示的當(dāng)前頁面(滑動(dòng)到一半位置,則顯示下一頁)
self.pageControl.currentPage = (int)(page+0.5);
}
@end
效果圖