利用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 = ;
CGFloat scrollY = ;
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(, );
} - (void)addScrollView
{
//添加3个view
for(int i = ; i < ; i++)
{
CGFloat viewX = i * [UIScreen mainScreen].bounds.size.width;
CGFloat viewY = ;
CGFloat viewW = [UIScreen mainScreen].bounds.size.width;
CGFloat viewH = [UIScreen mainScreen].bounds.size.height - ;
YCView *v = [[YCView alloc] initWithFrame:CGRectMake(viewX, viewY, viewW, viewH)];
v.backgroundColor = [UIColor colorWithRed:arc4random_uniform()/ 255.0 green:arc4random_uniform()/ 255.0 blue:arc4random_uniform()/ 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 * , [UIScreen mainScreen].bounds.size.height);
//设置偏移量
self.scrollV.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, );
//取消scrollView滚动到边缘的弹簧效果
self.scrollV.bounces = NO;
//隐藏水平滚动条
self.scrollV.showsHorizontalScrollIndicator = NO;
} #pragma mark --UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//设置按钮被选中状态下的颜色
scrollView.contentOffset.x == ? [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) * ? [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:];
}];
} - (IBAction)partBtnClick:(id)sender {
//跳转到第2个view contentOffset.x = 屏幕的宽度 * 1
//重置scrollView的位置
[UIView animateWithDuration:0.5 animations:^{
self.scrollV.contentOffset = [self ScrollViewWithContentOffSetPage:];
}]; } - (IBAction)serverBtnClick:(id)sender {
//跳转到第3个view contentOffset.x = 屏幕的宽度 * 2
//重置scrollView的位置
[UIView animateWithDuration:0.5 animations:^{
self.scrollV.contentOffset = [self ScrollViewWithContentOffSetPage:];
}];
} //返回scrollView偏移量
- (CGPoint)ScrollViewWithContentOffSetPage:(NSInteger)page{
return CGPointMake(([UIScreen mainScreen].bounds.size.width) * page, );
} - (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(, -, 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, , ([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 = ; i < ; i++)
{
for(int j = ; j < ; j++)
{
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(j * (ViewWeight + ViewMagin), i * (ViewHeight + ViewMagin), ViewWeight, ViewHeight)];
v.backgroundColor = [UIColor colorWithRed:arc4random_uniform()/ 255.0 green:arc4random_uniform()/ 255.0 blue:arc4random_uniform()/ 255.0 alpha:1.0];
[self addSubview:v];
}
}
} @end
利用UIScrollView实现几个页面的切换的更多相关文章
- Android开发之利用ViewPager实现页面的切换(仿微信、QQ)
这里利用ViewPager实现页面的滑动,下面直接上代码: 1.首先写一个Activity,然后将要滑动的Fragment镶嵌到写好的Activity中. Activity的布局文件:activity ...
- Swift - 使用UIScrollView实现页面滚动切换
UIScrollView提供了以页面为单位滚动显示各个子页面内容的功能,每次手指滑动后会滚动一屏的内容. 要实现该功能,需要如下操作: 1,将UIScrollView的pagingEnabled属 ...
- 前端性能优化之利用 Chrome Dev Tools 进行页面性能分析
背景 我们经常使用 Chrome Dev Tools 来开发调试,但是很少知道怎么利用它来分析页面性能,这篇文章,我将详细说明怎样利用 Chrome Dev Tools 进行页面性能分析及性能报告数据 ...
- Qt学习笔记(2)-利用StackWidget实现选项卡式页面
学习笔记第二篇,利用Qt实现选项卡式的页面,效果如图1.1-图1.3所示.程序实现的功能是通过点击状态栏实现不同页面的切换,实际上Qt中自带有Tab选项卡式的控件,本文利用StackWidge实现类似 ...
- ViewPager+Fragment实现页面的切换
新知识,新摘要: 效果图:framgent导入包都是v4包下,谨慎导入错误! 首先设置viewPager布局: <?xml version="1.0" encoding=&q ...
- 利用Chrome插件向指定页面植入js,劫持 XSS
资源来自:http://www.2cto.com/Article/201307/225986.html 首页 > 安全 > 网站安全 > 正文 利用Chrome插件向指定页面植入js ...
- 利用curl并发来提高页面访问速度
在我们平时的程序中难免出现同时访问几个接口的情况,平时我们用curl进行访问的时候,一般都是单个.顺序访问,假如有3个接口,每个接口耗时500毫 秒那么我们三个接口就要花费1500毫秒了,这个问题太头 ...
- 利用Bootstrap框架制作查询页面的界面
UI设计实战篇——利用Bootstrap框架制作查询页面的界面 Bootstrap框架是一个前端UI设计的框架,它提供了统一的UI界面,简化了设计界面UI的过程(缺点是定制了界面,调整的余地不是太 ...
- ViewPager和View组合 实现页面的切换
//--------------主页面------------------------------- package com.bw.test; import java.util.ArrayList;i ...
随机推荐
- (转)linux内核虚拟文件系统浅析
转自http://hi.baidu.com/_kouu/item/4e9db87580328244ef1e53d0 ###### 虚拟文件系统(VFS)在我看来, "虚拟"二字主要 ...
- position 属性和 z-index 属性对页面节点层级影响的例子
转:http://www.neoease.com/tutorials/z-index/ 不设 z-index 属性 单层节点 双层节点 多层节点
- java中产生对象的两种方式
/* * 普通new对象的过程! */ Person pp = new Person(); System.out.println(pp); /* * 利用代用参数的构造器产生对象实例! * 首先获得相 ...
- 2014 网选 广州赛区 hdu 5025 Saving Tang Monk(bfs+四维数组记录状态)
/* 这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态! 以前做过的都是用二维的!自己的四维还是太狭隘了..... 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找到! 每种钥匙有 k ...
- 受限玻尔兹曼机(RBM)学习笔记(六)对比散度算法
去年 6 月份写的博文<Yusuke Sugomori 的 C 语言 Deep Learning 程序解读>是囫囵吞枣地读完一个关于 DBN 算法的开源代码后的笔记,当时对其中涉及的算 ...
- 三元组表压缩存储稀疏矩阵实现稀疏矩阵的快速转置(Java语言描述)
三元组表压缩存储稀疏矩阵实现稀疏矩阵的快速转置(Java语言描述) 用经典矩阵转置算法和普通的三元组矩阵转置在时间复杂度上都是不乐观的.快速转置算法在增加适当存储空间后实现快速转置具体原理见代码注释部 ...
- [git]解决rebase冲突
git pull --rebase时产生冲突 有三个选项: git rebase --skip 效果是:抛弃本地的commit,采用远程的commit(慎用因为你本地的修改就会都没有!) git re ...
- Android的Activity生命周期
Android的Activity就相当于Windows Form中的Form,它的创建和销毁也是有一个生命周期的.主要经过这么7个阶段: 创建Activity:onCreate() 启动Activ ...
- DataBase --- Intellij IDEA 14.1.4使用Java连接SQL Server教程
Java连接数据库的方法大体分为两种:正向连接和反向连接.反向连接需要编译器提供相关的插件来支持,目前主流的java IDE都支持反向连接.这里主要对正向连接做一个经验总结. 一.数据库的配置 1.新 ...
- asp.net MVC ajax上传文件
普通上传 view: <body> <form id="form1" method="post" action="@Url.Acti ...