詳解iOS App中UISwitch開關(guān)組件的基本創(chuàng)建及使用方法
一、第一種創(chuàng)建UISwitch組件的方法,在代碼中動(dòng)態(tài)創(chuàng)建。
1、打開Xcode, 新建項(xiàng)目Switch,選擇Single View Application。
2、打開ViewController.m文件在viewDidLoad方法里添加代碼:
(void)viewDidLoad
{
[super viewDidLoad];
UISwitch *switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(50, 100, 20, 10)];
[switchButton setOn:YES];
[switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:switchButton];
// Do any additional setup after loading the view, typically from a nib.
}
[switchButton addTarget:selfaction:@selector(switchAction:)forControlEvents:UIControlEventValueChanged];
代碼中selector中的switchAction:需要我們自己實(shí)現(xiàn),就是按下時(shí)接收到的事件。
記得把switchButton加到當(dāng)前view,調(diào)用[self.viewaddSubview:switchButton];
3、監(jiān)聽UISwitch按下事件
實(shí)現(xiàn)代碼如下:
(void)switchAction:(id)sender
{
UISwitch *switchButton = (UISwitch*)sender;
BOOL isButtonOn = [switchButton isOn];
if (isButtonOn) {
showSwitchValue.text = @"是";
}else {
showSwitchValue.text = @"否";
}
}
showSwitchValue是我通過拖拽控件方法放到界面上的Label,方便顯示效果
運(yùn)行,效果:
二、通過拖拽方法使用UISwitch
1、往xib文件上拖拽一個(gè)UISwitch控件。
2、按alt+command + return鍵開啟Assistant Editor模式,選中UISwitch控件,按住Control鍵,往ViewController.h拖拽
3、選Action方式
4、.m文件中實(shí)現(xiàn)switchAction 。剛才動(dòng)態(tài)創(chuàng)建的時(shí)候也用到這個(gè)方法名稱,可以先注釋掉剛才的。
(IBAction)switchAction:(id)sender {
UISwitch *switchButton = (UISwitch*)sender;
BOOL isButtonOn = [switchButton isOn];
if (isButtonOn) {
showSwitchValue.text = @"是";
}else {
showSwitchValue.text = @"否";
}
}
三、自定義UISwitch
1.使用類別擴(kuò)展UISwitch。
如下:
下面是UISwitch.h文件:
#import
@interface UISwitch (tagged)
+ (UISwitch *) switchWithLeftText: (NSString *) tag1 andRight: (NSString *) tag2;
@property (nonatomic, readonly) UILabel *label1;
@property (nonatomic, readonly) UILabel *label2;
@end
UISwitch.m文件:
#import "UISwitch-Extended.h"
#define TAG_OFFSET 900
@implementation UISwitch (tagged)
- (void) spelunkAndTag: (UIView *) aView withCount:(int *) count
{
for (UIView *subview in [aView subviews])
{
if ([subview isKindOfClass:[UILabel class]])
{
*count += 1;
[subview setTag:(TAG_OFFSET + *count)];
}
else
[self spelunkAndTag:subview withCount:count];
}
}
- (UILabel *) label1
{
return (UILabel *) [self viewWithTag:TAG_OFFSET + 1];
}
- (UILabel *) label2
{
return (UILabel *) [self viewWithTag:TAG_OFFSET + 2];
}
+ (UISwitch *) switchWithLeftText: (NSString *) tag1 andRight: (NSString *) tag2
{
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
int labelCount = 0;
[switchView spelunkAndTag:switchView withCount:&labelCount];
if (labelCount == 2)
{
[switchView.label1 setText:tag1];
[switchView.label2 setText:tag2];
}
return [switchView autorelease];
}
@end
2.還有一種方法,這種方法比較簡(jiǎn)單,但比較難懂,我不甚理解。
UISwitch *isFooOrBar=[[UISwitch alloc] init];
((UILabel )[[[[[[isFooOrBar subviews] lastObject] subviews] objectAtIndex:2] subviews]objectAtIndex:0]).text = @"Foo";
((UILabel *)[[[[[[isFooOrBar subviews] lastObject] subviews] objectAtIndex:2] subviews]objectAtIndex:1]).text = @"Bar";*
四、一些常用方法
獲得開關(guān)狀態(tài)
BOOL setting = switchView.isOn;
NSLog(@"%d",setting);
設(shè)置開關(guān)狀態(tài) NO關(guān)閉狀態(tài),YES打開狀態(tài)
[switchView setOn:setting animated:YES];
設(shè)置開光的切換
switchView.onTintColor = [UIColor orangeColor];
設(shè)置按鈕的顏色
switchView.thumbTintColor = [UIColor redColor];
開關(guān)控件邊框的顏色
switchView.tintColor = [UIColor purpleColor];
添加觸發(fā)事件
[switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
相關(guān)文章
iOS開發(fā)實(shí)現(xiàn)轉(zhuǎn)盤功能
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)實(shí)現(xiàn)轉(zhuǎn)盤功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04iOS實(shí)現(xiàn)支付寶螞蟻森林隨機(jī)按鈕及抖動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)支付寶螞蟻森林隨機(jī)按鈕及抖動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02iOS中模態(tài)Model視圖跳轉(zhuǎn)和Push視圖跳轉(zhuǎn)的需求實(shí)現(xiàn)方法
這篇文章主要介紹了iOS中模態(tài)Model視圖跳轉(zhuǎn)和Push視圖跳轉(zhuǎn)的需求實(shí)現(xiàn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12iOS實(shí)現(xiàn)相冊(cè)多選圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)相冊(cè)多選圖片上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08iOS開發(fā)中Quartz2D繪圖路徑的使用以及條紋效果的實(shí)現(xiàn)
這篇文章主要介紹了iOS開發(fā)中Quartz2D繪圖路徑的使用以及條紋效果的實(shí)現(xiàn),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11解決ios h5 input輸入框被輸入法彈出一塊區(qū)域的問題
今天小編就為大家分享一篇解決ios h5 input輸入框被輸入法彈出一塊區(qū)域的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08