利用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 ...
随机推荐
- DDD:Command模式的好处
背景 会有朋友问我为啥用命令模式(Command Pattern)组织应用层,先看看MartinFowler咋说:http://martinfowler.com/bliki/CommandOrient ...
- [SLAM] 02 Some algorithms of 3D reconstruction
链接:http://www.zhihu.com/question/29885222/answer/100043031 首先一切建立在相机模型 x = kPX 上 x,X分别代表图片和空间中的二维三 ...
- 【转载】让windows使用linux系统的命令
转载自:http://blog.chinaunix.net/uid-28336510-id-3824903.html 1.下载GnuWin32,GnuWin32的安装文件(GetGnuWin32-0. ...
- IIS负载均衡的NLB解决方案
在现行的许多网络应用中,有时一台服务器往往不能满足客户端的要求,此时只能通过增加服务器来解决问题.那么,有没有一个办法或者技术来解决此类问题呢?使用Windows 2000或Windows Serve ...
- 【分享】iOS功能界面漂亮的弹出框
STPopup 为 iPhone 和 iPad提供了 STPopupController UINavigationController 弹出的风格. 特性: Extend your view cont ...
- Spring总结——AOP、JDBC和事务的总结
1.上一次总结了 Spring 的核心三大组件(Core,Beans,Context),今天总结的 AOP.JDBC和事务都可以看成是核心三大组件的应用. 其中 Spring 的事务管理又以 AOP ...
- eclipse中 properties文件编码问题
1. Eclipse修改设置 项目中用到了配置文件,所以在Eclipse中新建.properties文件,文件中编辑了中文,在保存时Eclipse报出以下错误: 解决这个问题的方法: 依次选择: 菜单 ...
- P6 EPPM Installation and Configuration Guide 16 R1 April 2016
P6 EPPM Installation and Configuration Guide 16 R1 April 2016 Contents About Installing and ...
- WinForm公共控件
公共控件:1.Button:按钮 用户点击时触发事件 行为属性 Enabled -是否启用 Visible -是否隐藏2.CheckBox .CheckListBox - 复选框 复选框组 3.Com ...
- 关于Chrome Dev Tool
★注意点一:函数名,方法名要拼写准确;