IOS学习之segmented control
轉載請注明出處
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 隨意時刻,僅僅有一個切割是激活狀態的。有點像單選button。
開發環境:
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);
/*
這個是設置按下button時的顏色
*/
segmentedControl.tintColor = [UIColor colorWithRed:49.0 / 256.0 green:148.0 / 256.0 blue:208.0 / 256.0 alpha:1];
segmentedControl.selectedSegmentIndex = 0;//默認選中的button索引
/*
以下的代碼實同正常狀態和按下狀態的屬性控制,比方字體的大小和顏色等
*/
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都不在有效了
我們再寫一個button的事件響應函數,設置不同的背景圖片,例如以下:
-(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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用本地EDAS注册多个中台服务,供本地
- 下一篇: STM32F4 TIM(外设定时器)