函数的代码来至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 class] description] UTF8String],[[[aView superclass] description] UTF8String]);
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 subviews] lastObject];
}
- (UIView *) textHolder {
return [[[self slider] subviews] objectAtIndex:2];
}
- (UILabel *) leftLabel {
return [[[self textHolder] subviews] objectAtIndex:0];
}
- (UILabel *) rightLabel {
return [[[self textHolder] subviews] objectAtIndex:1];
}
- (void) setLeftLabelText: (NSString *) labelText {
[[self leftLabel] setText:labelText];
}
- (void) setRightLabelText: (NSString *) labelText {
[[self rightLabel] setText:labelText];
}
@end
下面是测试代码:
- (void)loadView
{
contentView = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
contentView.backgroundColor = [UIColor whiteColor];
UICustomSwitch *switchView = [[UICustomSwitch alloc] initWithFrame:CGRectZero];
[switchView setCenter:CGPointMake(160.0f,170.0f)];
[contentView addSubview:switchView];
[switchView release];
switchView = [[UICustomSwitch alloc] initWithFrame:CGRectZero];
[switchView setCenter:CGPointMake(160.0f,200.0f)];
[switchView setAlternateColors:YES];
[contentView addSubview:switchView];
[switchView release];
switchView = [[UICustomSwitch alloc] initWithFrame:CGRectZero];
[switchView setCenter:CGPointMake(160.0f,230.0f)];
[switchView setLeftLabelText: @"YES"];
[switchView setRightLabelText: @"NO"];
[contentView addSubview:switchView];
[switchView release];
switchView = [[UICustomSwitch alloc] initWithFrame:CGRectZero];
[switchView setCenter:CGPointMake(160.0f,260.0f)];
[switchView setLeftLabelText: @"ABC"];
[switchView setRightLabelText: @"DEF"];
[[switchView rightLabel] setFont:[UIFont fontWithName:@"Georgia" size:16.0f]];
[[switchView leftLabel] setFont:[UIFont fontWithName:@"Georgia" size:16.0f]];
[[switchView leftLabel] setTextColor:[UIColor yellowColor]];
[contentView addSubview:switchView];
[switchView release];
self.view = contentView;
}
这样子定制后的结果老强大了(图1)。
图 1 图2
最后,悲催的事情还是发生了,我们选择了使用资源给出的两张图(图2),结果是我们放弃了那个平滑切换的动画把它做成了一个按钮,点一下换一张图片,记住一个状态。
分享到:
相关推荐
自定义标签选项视图,继承于UIControl,使用时与UIButton用法相似,可自定义视图颜色,文字颜色,选中与未选中色系,选择线或块,里面有些方法和使用的类报错正常,去掉就可以了。
ios应用源码之使用coreanimation自定义的uicontrol .
IOS应用源码——使用CoreAnimation自定义的UIControl.rar
IOS应用源码之使用CoreAnimation自定义的UIControl .rar
使用CoreAnimation自定义的UIControl.zipIOS应用例子源码下载使用CoreAnimation自定义的UIControl.zipIOS应用例子源码下载 1.适合学生学习研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
IOS应用源码——使用CoreAnimation自定义的UIControl.zip
一对向上/向下按钮可以链接到另一个 uicontrol 对象的“字符串”和“值”属性,允许按钮增加或减少它们的值。
本书全面探讨了 iphone平台的两种编程方式 ——web开发和 sdk编程。全书结合示例对这两种编程方式的基本流程、基本原理和基本原则给出了详细而通俗的讲解。在 web开发方面,分别介绍了三个 iphone web库,即 ...
(共分4个part,请下载完4个part后再解压) 目录 第1章iPhone SDK 简介......1 ...UIControl 类为许多iPhone 交互式元素提供了基础,包括按钮、文本字段、滑块和开关。第8章通过 已经或尚未用文档细致记录...
(共分4个part,请下载完4个part后再解压) 目录 第1章iPhone SDK 简介......1 ...UIControl 类为许多iPhone 交互式元素提供了基础,包括按钮、文本字段、滑块和开关。第8章通过 已经或尚未用文档细致记录...
(共分4个part,请下载完4个part后再解压) 目录 第1章iPhone SDK 简介......1 ...UIControl 类为许多iPhone 交互式元素提供了基础,包括按钮、文本字段、滑块和开关。第8章通过 已经或尚未用文档细致记录...
但是,Matlab 不会为 uicontrol 对象公开 uicontrol 的“VerticalAlignment”设置。 这是修复。 FIXUILABELS 遍历包含在当前 MATLAB 图窗中的 java 对象,并为所有“文本”样式的 uicontrol 调整底层 java 对象,...
该对象可以是以下之一:按钮、复选框、单选按钮、切换按钮、滑块、列表框、弹出菜单。 将适当设置对象的Value属性,并调用对象的Callback(如果将对象的Enable属性设置为OFF或INACTIVE,则将调用ButtonDownFcn)。 ...
这个自定义的UIControl类似于Twitter的新的基于进度的Web UI,它表示剩余的字符数。 它还可以突出显示UITextView中的所有多余字符。 屏幕截图 这是一个动画截图,向您展示了它的工作原理。 例 要运行示例项目,请...
本Demo是在继承UIControl的基础上,绘制出一个类似UILabel 的控件,可以实现文字的显示,文字颜色、大小的设置。
UIControl是诸如UIButton、UISwitch、UITextField等控件的父类,它本身也包含了一些属性和方法,但是不能直接使用UIControl类,它只是定义了子类都需要使用的方法。 UIControl对象采用了一种新的事件处理机制,将前...
自定义UIControl控件的例子。 功能:通过手势滑动,调节进度条的进度。通过如果进度条改变了,触发UIControlChangeValue消息。
前两年IOS应用源码-主要是用于毕业设计学习的。