1.今天學習了一下UIScrollveiw的使用方法:就是在歡迎界面的使用來介紹產品的新特性的代碼:ide
下面有兩種方法來實現其中代碼:(很少說廢話,直接來代碼)使用xib實現的學習
#import "WecomeViewController1.h"atom
#import "AppDelegate.h"spa
#import "QuestionViewController.h"代理
@interface WecomeViewController1 ()<UIScrollViewDelegate>orm
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;blog
@property (weak, nonatomic) IBOutlet UIPageControl *pagecount;圖片
@endget
@implementation WecomeViewController1string
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpscrollview];
[self setUppageview];
}
-(void)setUpscrollview{
self.scrollview.delegate = self; //設置代理
//設置圖片
UIImageView *imageview1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cover1.png"]];
UIImageView *imageview2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cover2.png"]];
UIImageView *imageview3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cover3.png"]];
//設置frmae
imageview1.frame = CGRectMake(0, 0, screenWitdth, screenHeight);
imageview2.frame = CGRectMake(screenWitdth, 0, screenWitdth, screenHeight);
imageview3.frame = CGRectMake(2*screenWitdth, 0, screenWitdth, screenHeight);
//填充模式
imageview1.contentMode = UIViewContentModeScaleAspectFit;
imageview2.contentMode = UIViewContentModeScaleAspectFit;
imageview3.contentMode = UIViewContentModeScaleAspectFit;
//在第三張圖片上面加一個button
imageview3.userInteractionEnabled = YES;
UIButton *customButton = [[UIButton alloc]init];
[customButton setBackgroundImage:[UIImage imageNamed:@"start1.png"] forState:UIControlStateNormal];
CGFloat centerX = imageview3.frame.size.width * 0.5;
CGFloat centerY = imageview3.frame.size.height * 0.9;
customButton.center = CGPointMake(centerX, centerY);
customButton.bounds = (CGRect){CGPointZero, 100 ,40};
[customButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
[imageview3 addSubview:customButton];
//將圖添加到scrllview上面去
[self.scrollview addSubview:imageview1];
[self.scrollview addSubview:imageview2];
[self.scrollview addSubview:imageview3];
//設置scrollvew的一些屬性
self.scrollview.contentSize = CGSizeMake(3*screenWitdth, 0);
self.scrollview.pagingEnabled = YES;
self.scrollview.showsHorizontalScrollIndicator = NO;
self.scrollview.showsVerticalScrollIndicator = NO;
self.scrollview.bounces = NO;
}
-(void)login{
NSLog(@"登錄界面");
QuestionViewController *questVC = [[QuestionViewController alloc]init];
[self presentViewController:questVC animated:YES completion:nil];
}
//建立pagecontoller
-(void)setUppageview{
self.pagecount.numberOfPages = 3;
self.pagecount.currentPageIndicatorTintColor = [UIColor grayColor];
self.pagecount.pageIndicatorTintColor = [UIColor blackColor];
}
#pragma mark -UIScrollDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//計算水平的距離
CGFloat offsetX = self.scrollview.contentOffset.x;
//求頁碼
double pageDouble = offsetX / scrollView.frame.size.width;
int pageInt = (int)(pageDouble + 0.5);
self.pagecount.currentPage = pageInt;
}
@end
第二種純代碼實現:
#define CWNewfeatureImageCount 3
#import "CWNewfeatureViewController.h"
#import "CWTabBarViewController.h"
@interface CWNewfeatureViewController ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIPageControl *pageControl;
@end
@implementation CWNewfeatureViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.添加UISrollView
[self setupScrollView];
// 2.添加pageControl
[self setupPageControl];
}
/**
* 添加pageControl
*/
- (void)setupPageControl
{
// 1.添加
UIPageControl *pageControl = [[UIPageControl alloc] init];
pageControl.numberOfPages = CWNewfeatureImageCount;
CGFloat centerX = self.view.frame.size.width * 0.5;
CGFloat centerY = self.view.frame.size.height - 30;
pageControl.center = CGPointMake(centerX, centerY);
pageControl.bounds = CGRectMake(0, 0, 100, 30);
pageControl.userInteractionEnabled = NO;
[self.view addSubview:pageControl];
self.pageControl = pageControl;
// 2.設置圓點的顏色
pageControl.currentPageIndicatorTintColor = CWColor(253, 98, 42);
pageControl.pageIndicatorTintColor = CWColor(189, 189, 189);
}
-(void)setupScrollView
{
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = self.view.bounds;
scrollView.delegate = self;
[self.view addSubview:scrollView];
// 2.添加圖片
CGFloat imageW = scrollView.frame.size.width;
CGFloat imageH = scrollView.frame.size.height;
for (int index = 0; index<CWNewfeatureImageCount; index++) {
UIImageView *imageView = [[UIImageView alloc] init];
// 設置圖片
NSString *name = nil;
if (fourInch) {
name = [NSString stringWithFormat:@"new_feature_%d-568h", index + 1];
} else {
name = [NSString stringWithFormat:@"new_feature_%d", index + 1];
}
imageView.image = [UIImage imageWithName:name];
// 設置frame
CGFloat imageX = index * imageW;
imageView.frame = CGRectMake(imageX, 0, imageW, imageH);
[scrollView addSubview:imageView];
// 在最後一個圖片上面添加按鈕
if (index == CWNewfeatureImageCount - 1) {
[self setupLastImageView:imageView];
}
}
// 3.設置滾動的內容尺寸
scrollView.contentSize = CGSizeMake(imageW * CWNewfeatureImageCount, 0);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.pagingEnabled = YES;
scrollView.bounces = NO;
}
/**
* 添加內容到最後一個圖片
*/
- (void)setupLastImageView:(UIImageView *)imageView
{
// 0.讓imageView能跟用戶交互
imageView.userInteractionEnabled = YES;
// 1.添加開始按鈕
UIButton *startButton = [[UIButton alloc] init];
[startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button"] forState:UIControlStateNormal];
[startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
// 2.設置frame
CGFloat centerX = imageView.frame.size.width * 0.5;
CGFloat centerY = imageView.frame.size.height * 0.6;
startButton.center = CGPointMake(centerX, centerY);
startButton.bounds = (CGRect){CGPointZero, startButton.currentBackgroundImage.size};
// 3.設置文字
[startButton setTitle:@"開始微博" forState:UIControlStateNormal];
[startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:startButton];
// 4.添加checkbox
UIButton *checkbox = [[UIButton alloc] init];
checkbox.selected = YES;
[checkbox setTitle:@"分享給你們" forState:UIControlStateNormal];
[checkbox setImage:[UIImage imageWithName:@"new_feature_share_false"] forState:UIControlStateNormal];
[checkbox setImage:[UIImage imageWithName:@"new_feature_share_true"] forState:UIControlStateSelected];
checkbox.bounds = CGRectMake(0, 0, 200, 50);
CGFloat checkboxCenterX = centerX;
CGFloat checkboxCenterY = imageView.frame.size.height * 0.5;
checkbox.center = CGPointMake(checkboxCenterX, checkboxCenterY);
[checkbox setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
checkbox.titleLabel.font = [UIFont systemFontOfSize:15];
[checkbox addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
checkbox.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
[imageView addSubview:checkbox];
}
- (void)checkboxClick:(UIButton *)checkbox
{
checkbox.selected = !checkbox.isSelected;
}
/**
* 開始微博
*/
- (void)start
{
// 顯示狀態欄
[UIApplication sharedApplication].statusBarHidden = NO;
// 切換窗口的根控制器
self.view.window.rootViewController = [[CWTabBarViewController alloc] init];
}
/**
* 只要UIScrollView滾動了,就會調用
*
*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 1.取出水平方向上滾動的距離
CGFloat offsetX = scrollView.contentOffset.x;
// 2.求出頁碼
double pageDouble = offsetX / scrollView.frame.size.width;
int pageInt = (int)(pageDouble + 0.5);
self.pageControl.currentPage = pageInt;
}
@end
效果圖: