UISwitch开关控件


 

1.UISwitch的初始化

UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(54.0f, 16.0f, 100.0f, 28.0f)];

2.设置UISwitch的初始化状态

switchView.on = YES;//设置初始为ON的一边

 3.UISwitch事件的响应

[switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];

.h文件中声明


@interface UIswitchViewController :UIViewController

{

    UISwitch* leftSwitch;

    UISwitch* rightSwitch;

}

@property(nonatomic,retain)UISwitch*leftSwitch;

@property(nonatomic,retain)UISwitch*rightSwitch;

.m文件

@synthesize leftSwitch,rightSwitch;


- (void)viewDidLoad

{

    [superviewDidLoad];

   leftSwitch=[[UISwitchalloc]initWithFrame:CGRectMake(0, 0, 40, 20)];//创建

   rightSwitch=[[UISwitchallocinitWithFrame:CGRectMake(0,240, 40, 20)];

    [leftSwitchaddTarget:selfaction:@selector(switchChanged:)forControlEvents:UIControlEventValueChanged];//函数调用

    [self.viewaddSubview:leftSwitch];     //添加到试图上

    [rightSwitchaddTarget:selfaction:@selector(switchChanged:)forControlEvents:UIControlEventValueChanged];

    [self.viewaddSubview:rightSwitch];

// Do any additional setup after loading the view.

}

- (IBAction)switchChanged:(id)sender {

    UISwitch *mySwitch = (UISwitch *)sender;

    BOOL setting = mySwitch.isOn;//获得开关状态

    if(setting)

    {

       NSLog(@"YES");

    }else {

       NSLog(@"NO");

    }

    [leftSwitchsetOn:setting animated:YES];//设置开关状态

    [rightSwitchsetOn:setting animated:YES];

}



自定义开关效果


函数的代码来至iphone开发秘籍,Thanks Erica Sadun。

UISwitch类:

 

UISwitch类的单薄到我不知道该说什么了。不过,UIControl对象通常是由一系列的子视图构建的。通过导航控件的视图,可以公开的定制通常不能从标准SDK中访问的对象。这种定制依赖于对控件子视图树的理解,通过下面这样的函数可以递归遍历视图树,就可以了解每一个视图了。

- (void)explode:(id)aView level:(int)aLevel {

         for (int i = 0; i < aLevel; i++)

                   printf("-");

         printf("%s:%s/n",[[[aView classdescriptionUTF8String],[[[aView superclassdescriptionUTF8String]);

        

         for(UIView *subview in [aView subviews])

                   [self explode:subview level:(aLevel + 1)];

}

初始化级别为0,打出来的结果是:

UISwitch:UIControl

-_UISwitchSlider:UISlider

--UIImageView:UIView

--UIImageView:UIView

--UIView:UIResponder

---UILabel:UIView

---UILabel:UIView

--UIImageView:UIView

 

然后就可以开始封装自定义UISwitch字体和字体颜色的定制功能

@interface UISwitch (extended)

- (void) setAlternateColors:(BOOL) boolean;//这是文档未记录的特性,显示为橘黄色的背景。

@end

 

@interface _UISwitchSlider : UIView

@end

 

@interface UICustomSwitch : UISwitch

- (void) setLeftLabelText: (NSString *) labelText;

- (void) setRightLabelText: (NSString *) labelText;

@end

 

@implementation UICustomSwitch

- (_UISwitchSlider *) slider {

         return [[self subviewslastObject];

}

- (UIView *) textHolder {

         return [[[self slidersubviewsobjectAtIndex:2];

}

- (UILabel *) leftLabel {

         return [[[self textHoldersubviewsobjectAtIndex:0];

}

- (UILabel *) rightLabel {

         return [[[self textHoldersubviewsobjectAtIndex:1];

}

- (void) setLeftLabelText: (NSString *) labelText {

         [[self leftLabelsetText:labelText];

}

- (void) setRightLabelText: (NSString *) labelText {

         [[self rightLabelsetText:labelText];

}

@end

下面是测试代码:

- (void)loadView

{

         contentView = [[[UIView allocinitWithFrame:[[UIScreen mainScreenapplicationFrame]] autorelease];

         contentView.backgroundColor = [UIColor whiteColor];

        

         UICustomSwitch *switchView = [[UICustomSwitch allocinitWithFrame:CGRectZero];

         [switchView setCenter:CGPointMake(160.0f,170.0f)];

         [contentView addSubview:switchView];

         [switchView release];

 

         switchView = [[UICustomSwitch allocinitWithFrame:CGRectZero];

         [switchView setCenter:CGPointMake(160.0f,200.0f)];

         [switchView setAlternateColors:YES];

         [contentView addSubview:switchView];

         [switchView release];

        

         switchView = [[UICustomSwitch allocinitWithFrame:CGRectZero];

         [switchView setCenter:CGPointMake(160.0f,230.0f)];

         [switchView setLeftLabelText@"YES"];

         [switchView setRightLabelText@"NO"];

         [contentView addSubview:switchView];

         [switchView release];

        

         switchView = [[UICustomSwitch allocinitWithFrame:CGRectZero];

         [switchView setCenter:CGPointMake(160.0f,260.0f)];

         [switchView setLeftLabelText@"ABC"];

         [switchView setRightLabelText@"DEF"];

         [[switchView rightLabelsetFont:[UIFont fontWithName:@"Georgia" size:16.0f]];

         [[switchView leftLabelsetFont:[UIFont fontWithName:@"Georgia" size:16.0f]];

         [[switchView leftLabelsetTextColor:[UIColor yellowColor]];

         [contentView addSubview:switchView];

         [switchView release];     

        

         self.view = contentView;

}

这样子定制后的结果老强大了(图1)。

                     

图 1                                                                                             图2

最后,悲催的事情还是发生了,我们选择了使用资源给出的两张图(图2),结果是我们放弃了那个平滑切换的动画把它做成了一个按钮,点一下换一张图片,记住一个状态。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值