国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

利用UIScrollView實現幾個頁面的切換

2019-11-14 18:19:57
字體:
來源:轉載
供稿:網友

此實例可以了解一下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

 


上一篇:copy

下一篇:iOS-通信錄

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 滁州市| 元朗区| 泰来县| 峨边| 华池县| 太白县| 华容县| 泰兴市| 崇仁县| 南投市| 望江县| 泸西县| 宣恩县| 景东| 沂南县| 阳城县| 横峰县| 鸡东县| 固安县| 府谷县| 宿松县| 屯门区| 塘沽区| 余干县| 扶余县| 梨树县| 靖宇县| 定安县| 高青县| 图木舒克市| 广灵县| 垫江县| 永清县| 建平县| 梁平县| 屏南县| 屏南县| 清水河县| 呼伦贝尔市| 呼和浩特市| 黑河市|