利用UIScrollView實現幾個頁面的切換,uiscrollview頁面
此執行個體可以瞭解一下UIScrollView的運用,以及表格跟頁面跳轉的內容;
原作者地址:http://www.cocoachina.com/bbs/read.php?tid=323514
如下:
1:知識點滾動視圖的運用
#import "YCView.h"@interface ViewController ()<UIScrollViewDelegate>@property (nonatomic, strong)UIScrollView *scrollV;@property (weak, nonatomic) IBOutlet UIButton *usesbtn;@property (weak, nonatomic) IBOutlet UIButton *partBtn;@property (weak, nonatomic) IBOutlet UIButton *serverBtn;@end@implementation ViewController//懶載入- (UIScrollView *)scrollV{ if(!_scrollV) { _scrollV = [[UIScrollView alloc] init]; //設定scrollView的frame CGFloat scrollX = 0; CGFloat scrollY = 110; CGFloat scrollW = CGRectGetWidth(self.view.bounds); CGFloat scrollH = CGRectGetHeight(self.view.bounds); _scrollV.frame = CGRectMake(scrollX, scrollY, scrollW, scrollH); //設定代理 _scrollV.delegate = self; //將scrollView添加到控制器的view上 [self.view addSubview:_scrollV]; } return _scrollV;}- (void)viewDidLoad { [super viewDidLoad]; //添加視圖 view [self addScrollView]; self.scrollV.contentOffset = CGPointMake(0, 0);}- (void)addScrollView{ //添加3個view for(int i = 0; i < 3; i++) { CGFloat viewX = i * [UIScreen mainScreen].bounds.size.width; CGFloat viewY = 0; CGFloat viewW = [UIScreen mainScreen].bounds.size.width; CGFloat viewH = [UIScreen mainScreen].bounds.size.height - 108; YCView *v = [[YCView alloc] initWithFrame:CGRectMake(viewX, viewY, viewW, viewH)]; v.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/ 255.0 green:arc4random_uniform(255)/ 255.0 blue:arc4random_uniform(255)/ 255.0 alpha:1.0]; [self.scrollV addSubview:v]; } //設定frame,位移量 //設定分頁 self.scrollV.pagingEnabled = YES; self.scrollV.backgroundColor = [UIColor orangeColor]; //設定滾動範圍 self.scrollV.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 3, [UIScreen mainScreen].bounds.size.height); //設定位移量 self.scrollV.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0); //取消scrollView滾動到邊緣的彈簧效果 self.scrollV.bounces = NO; //隱藏水平捲軸 self.scrollV.showsHorizontalScrollIndicator = NO;}#pragma mark --UIScrollViewDelegate- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ //設定按鈕被選中狀態下的顏色 scrollView.contentOffset.x == 0 ? [self.usesbtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal] : [self.usesbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; scrollView.contentOffset.x == ([UIScreen mainScreen].bounds.size.width) ? [self.partBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal] : [self.partBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; scrollView.contentOffset.x == ([UIScreen mainScreen].bounds.size.width) * 2 ? [self.serverBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal] : [self.serverBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];}#pragma mark --btnClick- (IBAction)usesBtnClick:(id)sender { //跳轉到第1個view contentOffset.x = 螢幕的寬度 * 0 //重設scrollView的位置 [UIView animateWithDuration:0.5 animations:^{ self.scrollV.contentOffset = [self ScrollViewWithContentOffSetPage:0]; }];}- (IBAction)partBtnClick:(id)sender { //跳轉到第2個view contentOffset.x = 螢幕的寬度 * 1 //重設scrollView的位置 [UIView animateWithDuration:0.5 animations:^{ self.scrollV.contentOffset = [self ScrollViewWithContentOffSetPage:1]; }]; }- (IBAction)serverBtnClick:(id)sender { //跳轉到第3個view contentOffset.x = 螢幕的寬度 * 2 //重設scrollView的位置 [UIView animateWithDuration:0.5 animations:^{ self.scrollV.contentOffset = [self ScrollViewWithContentOffSetPage:2]; }];}//返回scrollView位移量- (CGPoint)ScrollViewWithContentOffSetPage:(NSInteger)page{ return CGPointMake(([UIScreen mainScreen].bounds.size.width) * page, 0);}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
2:列表及跳轉跳顯示的內容
#import "YCView.h"#import "YCCellView.h"static NSString *idenifer = @"YCCollectionViewCell";#define CellWeigth ([UIScreen mainScreen].bounds.size.width)/3.0#define CellHeigth 44@interface YCView ()<UITableViewDataSource, UITableViewDelegate>@property (strong, nonatomic)NSArray *parts;@property (strong, nonatomic)NSMutableArray *Views;@end@implementation YCView//懶載入- (NSMutableArray *)Views{ if(!_Views){ _Views = [NSMutableArray array]; } return _Views;}//懶載入- (NSArray *)parts{ if(!_parts) { _parts = [NSArray array]; _parts = @[@"熱門推薦", @"汽車外飾", @"香水/淨化", @"功能用品", @"美容養護", @"安全/防護", @"影音導航"]; } return _parts;}- (instancetype)init{ if(self = [super init]) { [self addView]; } return self;}- (instancetype)initWithFrame:(CGRect)frame{ if(self = [super initWithFrame:frame]) { [self addView]; } return self;}- (void)addView{ //添加tableView UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -44, CellWeigth, [UIScreen mainScreen].bounds.size.height) style:UITableViewStyleGrouped]; tableView.backgroundColor = [UIColor redColor]; tableView.dataSource = self; tableView.delegate = self; [self addSubview:tableView];}#pragma mark --UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.parts.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"YCCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } cell.textLabel.text = self.parts[indexPath.row]; return cell;}#pragma mark --UITableViewDelegate- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [self addBestView];}- (void)addBestView{ YCCellView *view = [[YCCellView alloc] initWithFrame:CGRectMake(CellWeigth, 0, ([UIScreen mainScreen].bounds.size.width)-CellWeigth, [UIScreen mainScreen].bounds.size.height)]; view.backgroundColor = [UIColor redColor]; [[self.Views lastObject] removeFromSuperview]; [self.Views addObject:view]; [self addSubview:view];}- (void)layoutSubviews{ [super layoutSubviews];}@end
3:單元列的內容
#import "YCCellView.h"#define ViewMagin 10#define ViewHeight 90#define ViewWeight (([UIScreen mainScreen].bounds.size.width)-CellWeigth - 3*ViewMagin)/3.0#define CellWeigth ([UIScreen mainScreen].bounds.size.width)/3.0@interface YCCellView ()@end@implementation YCCellView- (instancetype)init{ if(self = [super init]) { [self addCollectionView]; } return self;}- (instancetype)initWithFrame:(CGRect)frame{ if(self = [super initWithFrame:frame]) { [self addCollectionView]; } return self;}- (void)addCollectionView{ for(int i = 0; i < 4; i++) { for(int j = 0; j < 3; j++) { UIView *v = [[UIView alloc] initWithFrame:CGRectMake(j * (ViewWeight + ViewMagin), i * (ViewHeight + ViewMagin), ViewWeight, ViewHeight)]; v.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/ 255.0 green:arc4random_uniform(255)/ 255.0 blue:arc4random_uniform(255)/ 255.0 alpha:1.0]; [self addSubview:v]; } }}@end