IOS学习之segmented control
来源:互联网 发布:java图形界面框架 编辑:程序博客网 时间:2024/05/16 11:05
转载请注明出处
http://blog.csdn.net/pony_maggie/article/details/27086877
作者:小马
什么是segmented control? 先上几张图:
这几幅图就是典型的segmented control UI视图, 第一幅是某个游戏程序,红色框出来的就是segmentedcontrol。 后面三幅是我这篇博文做的demo示例。
segmented control有如下几个特征:
1通常是在单视图中使用,不做多视图之间的切换。实现视图中不同显示的快速切换,每一个分割表示一个不同的显示,这些显示往往是相关的,所谓的相关,可以理解成,功能一样,但是属性类别有差异,比如上图游戏程序中的几个分割。
比较常用的还有比如说,在一个视图中,不同的分割控制tableView加载不同的数据源。
2 它通常在整个屏幕的上部,不是必然,但大部分情况下是这样用。
3 一般是3到5个分割,超过5个的话每个分割的大小对于用户触碰的体验会很差。
4 任意时刻,只有一个分割是激活状态的。有点像单选按钮。
开发环境:
mac os +xcode5.0 + ios7模拟器。
生成控件,代码如下:
- (void)initSegmentedControl{ NSArray *segmentedData = [[NSArray alloc]initWithObjects:@"apple",@"orange",@"banana",nil]; UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedData]; segmentedControl.frame = CGRectMake(10.0, 20.0,300.0, 30.0); /* 这个是设置按下按钮时的颜色 */ segmentedControl.tintColor = [UIColor colorWithRed:49.0 / 256.0 green:148.0 / 256.0 blue:208.0 / 256.0 alpha:1]; segmentedControl.selectedSegmentIndex = 0;//默认选中的按钮索引 /* 下面的代码实同正常状态和按下状态的属性控制,比如字体的大小和颜色等 */ NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:12],NSFontAttributeName,[UIColor redColor], NSForegroundColorAttributeName, nil]; [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor redColor] forKey:NSForegroundColorAttributeName]; [segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted]; //设置分段控件点击相应事件 [segmentedControl addTarget:self action:@selector(doSomethingInSegment:)forControlEvents:UIControlEventValueChanged]; [self.view addSubview:segmentedControl];}
每个功能注释都有清晰的描述,有一点要特别说明一下:
在ios7以前,segmentedcontrol有一个segmentedControlStyle 属性,通常都要设置,比如像下面这样:
/* typedef enum { UISegmentedControlStylePlain, UISegmentedControlStyleBordered, UISegmentedControlStyleBar, UISegmentedControlStyleBezeled, } UISegmentedControlStyle; */segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
但是这个在ios7之后,出于扁平化风格的考虑,这些style都不在有效了
我们再写一个按钮的事件响应函数,设置不同的背景图片,如下:
-(void)doSomethingInSegment:(UISegmentedControl *)Seg{ NSInteger Index = Seg.selectedSegmentIndex; switch (Index) { case 0: self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:kSrcName(@"bg_apple_small.png")]]; break; case 1: self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:kSrcName(@"bg_orange_small.png")]]; break; case 2: self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:kSrcName(@"bg_banana_small.png")]]; break; default: break; }}
代码比较简单。关键是理解segmented control的应用场景,灵活运用。除了第一幅图中的游戏程序,我这里再举一个例子,很多时候会把segmented control嵌套在navigation bar里面使用,以减少navigationview之间的层级数量,给用户较好的体验,就像下面这样:
源码下载:
https://github.com/pony-maggie/SegmentedControl或
http://download.csdn.net/detail/pony_maggie/7403175
- IOS学习之segmented control
- IOS学习之segmented control
- iOS学习——Segmented Control
- 《iOS Human Interface Guidelines》——Segmented Control
- Android仿IOS的Segmented Control 选项卡。
- 自定义 Segmented Control
- 什么是segmented control
- segmented control的使用
- Segmented Control - 仿 app store的Segmented Control
- iPhone Segmented Control:UISegmentedControl Tutorial
- Scroll Segmented Control(Swift)
- 三十而立,从零开始学ios开发(六):IOS控件(3),Segmented Control、
- iPhone开发【十二】多视图技术总结之四:Segmented Control
- iPhone开发【十二】多视图技术总结之四:Segmented Control
- How to use the Segmented Control
- 一个定制SEGMENTED CONTROL的开源代码
- IOS深入学习(3)之Control Object
- TextView,TextField,slider,segmented Control等各类控件的使用
- 当天下午5时30分左右,市民小彭正在附近一地面美食街吃东西,发现到处人头涌动,人群很混乱,“大家都跑了起来,听到有人喊‘砍人啊’
- 调用cvCreateFileCapture引起的内存泄露
- Scripts:查看未提交的事务的脚本CurrentActivity.sql
- 回溯法解决排列A(m,n)
- 四种生成和解析XML文档的方法详解(介绍+优缺点比较)
- IOS学习之segmented control
- mcp2515带spi的can驱动移植总结
- Effective C++ Item 23 宁以non-member、non-friend替换member函数
- 春天来了?Win9和WP9将在2015年4月发布
- mongodb 高级应用
- YLGIFImage 高效读取GIF图片
- Ubuntu14.04_64安装与使用日记
- 机构自由度计算(笔记)
- 苹果Mac Pro提速停不下来