此实例可以了解一下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实现几个页面的切换的更多相关文章

  1. Android开发之利用ViewPager实现页面的切换(仿微信、QQ)

    这里利用ViewPager实现页面的滑动,下面直接上代码: 1.首先写一个Activity,然后将要滑动的Fragment镶嵌到写好的Activity中. Activity的布局文件:activity ...

  2. Swift - 使用UIScrollView实现页面滚动切换

    UIScrollView提供了以页面为单位滚动显示各个子页面内容的功能,每次手指滑动后会滚动一屏的内容.   要实现该功能,需要如下操作: 1,将UIScrollView的pagingEnabled属 ...

  3. 前端性能优化之利用 Chrome Dev Tools 进行页面性能分析

    背景 我们经常使用 Chrome Dev Tools 来开发调试,但是很少知道怎么利用它来分析页面性能,这篇文章,我将详细说明怎样利用 Chrome Dev Tools 进行页面性能分析及性能报告数据 ...

  4. Qt学习笔记(2)-利用StackWidget实现选项卡式页面

    学习笔记第二篇,利用Qt实现选项卡式的页面,效果如图1.1-图1.3所示.程序实现的功能是通过点击状态栏实现不同页面的切换,实际上Qt中自带有Tab选项卡式的控件,本文利用StackWidge实现类似 ...

  5. ViewPager+Fragment实现页面的切换

    新知识,新摘要: 效果图:framgent导入包都是v4包下,谨慎导入错误! 首先设置viewPager布局: <?xml version="1.0" encoding=&q ...

  6. 利用Chrome插件向指定页面植入js,劫持 XSS

    资源来自:http://www.2cto.com/Article/201307/225986.html 首页 > 安全 > 网站安全 > 正文 利用Chrome插件向指定页面植入js ...

  7. 利用curl并发来提高页面访问速度

    在我们平时的程序中难免出现同时访问几个接口的情况,平时我们用curl进行访问的时候,一般都是单个.顺序访问,假如有3个接口,每个接口耗时500毫 秒那么我们三个接口就要花费1500毫秒了,这个问题太头 ...

  8. 利用Bootstrap框架制作查询页面的界面

    UI设计实战篇——利用Bootstrap框架制作查询页面的界面   Bootstrap框架是一个前端UI设计的框架,它提供了统一的UI界面,简化了设计界面UI的过程(缺点是定制了界面,调整的余地不是太 ...

  9. ViewPager和View组合 实现页面的切换

    //--------------主页面------------------------------- package com.bw.test; import java.util.ArrayList;i ...

随机推荐

  1. vbox导入虚拟电脑网卡MAC问题

    vbox导入虚拟电脑之后,需要"重新初始化所有网卡的MAC地址". centos需要做以下几步操作: 1.删除文件"/etc/udev/rules.d/70-persis ...

  2. VS错误 error LNK1158: 无法运行“cvtres.exe”

    1.删除文件 2.如果删除不掉 右键属性 - 安全 - 高级 - 所有者 - 确定 右键属性 - 安全 - 编辑 - 开通权限

  3. MyBatis知多少(26)调试

    这是很容易,同时与iBATIS的工作程序进行调试. iBATIS有内置的日志支持,并适用于下列日志库,并在这个顺序搜索他们. Jakarta Commons日志记录(JCL). Log4J JDK 日 ...

  4. ServiceStack 介绍

    关于ServiceStack ServiceStack 官网介绍: Opensource .NET and Mono REST Web Services framework 什么是 ServiceSt ...

  5. IIS6.0启动网站ManagedPipelineHandler异常

    处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler” 开发web项目需要安装IIS,当安装完以后,web程 ...

  6. css优先级和层叠

    css优先级和层叠 1.优先级    计算方法:        a.行内样式        b.id选择器的数量        c.类,伪类和属性选择器的数量        d.标签选择器和伪元素选择 ...

  7. ok6410 android driver(11)

    This essay, I go to a deeply studying to android HAL device driver program. According to the android ...

  8. 译:泛型List集合转化为DateTable的扩展方法

    译文出处:http://www.codeproject.com/Tips/867866/Extension-Method-for-Generic-List-Collection-to-Da 这段代码是 ...

  9. Winform开发框架之权限管理系统功能介绍

    权限管理系统的重要特性总结: 1) 高度集成的权限系统.独立模块,能快速整合使用.2) 符合权限的国际通用标准,基于RBAC(基于角色的访问控制)的角色权限控制.3) 多数据库架构支持,内置支持Sql ...

  10. 【iOS】编译静态库

    与java和.net一样,objc也由类库的概念,不过在在objc上一般叫库,库表示程序代码集合,可以共享给其他程序使用,库是编译后的二进制文件,因此不能看到源代码,多用于一些开放sdk(如百度地图s ...