IOS——重写按钮(开关功能)
来源:互联网 发布:pvc管自制笛子开孔数据 编辑:程序博客网 时间:2024/06/06 18:05
我们想要定制按钮,可以在“开”和“关”之间切换,但是UISwitch又不符合我们的设计,这时候就得自定义这样的按钮,可以通过继承UIButton来实现。
XYToggleButton.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#import <UIKit/UIKit.h>
@interface XYToggleButton : UIButton
@property (nonatomic, getter = isOn)
BOOL
on;
@property (nonatomic, getter = isAutotoggleEnabled)
BOOL
autotoggleEnabled;
+ (id)buttonWithOnImage:(UIImage *)onImage
offImage:(UIImage *)offImage
highlightedImage:(UIImage *)highlightedImage;
- (
BOOL
)toggle;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#import "XYToggleButton.h"
@interface XYToggleButton ()
@property (nonatomic, retain) UIImage *onImage;
@property (nonatomic, retain) UIImage *offImage;
@end
@implementation XYToggleButton
@synthesize onImage = _onImage;
@synthesize offImage = _offImage;
@synthesize on = _on;
@synthesize autotoggleEnabled = _autotoggleEnabled;
+ (id)buttonWithOnImage:(UIImage *)onImage offImage:(UIImage *)offImage highlightedImage:(UIImage *)highlightedImage{
XYToggleButton *button;
button = [XYToggleButton buttonWithType:UIButtonTypeCustom];
button.onImage = onImage;
button.offImage = offImage;
[button setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
[button setBackgroundImage:offImage forState:UIControlStateNormal];
button.autotoggleEnabled = YES;
return
button;
}
- (
void
)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
[super endTrackingWithTouch:touch withEvent:event];
if
(self.touchInside && self.autotoggleEnabled) {
[self toggle];
}
}
- (
BOOL
)toggle{
self.on = !self.on;
return
self.on;
}
- (
void
)setOn:(
BOOL
)on{
if
(_on != on) {
_on = on;
[self setBackgroundImage:(_on ? self.onImage : self.offImage) forState:UIControlStateNormal];
}
}
//添加对IB的支持
#pragma mark - initFromNib
- (
void
)awakeFromNib{
self.autotoggleEnabled = YES;
self.onImage = [self backgroundImageForState:UIControlStateSelected];
self.offImage = [self backgroundImageForState:UIControlStateNormal];
[self setBackgroundImage:nil forState:UIControlStateSelected];
}
@end
0 0
- IOS——重写按钮(开关功能)
- IOS开发之UI篇—UISwitch(开关按钮)
- 【IOS 开发学习总结-OC-43】★ios开发——UIButton(按钮)与 UISwitch(开关按钮)
- Android自定义控件——仿ios开关按钮
- Android自定义控件——仿ios开关按钮
- Android自定义控件——仿ios开关按钮
- Android自定义控件——仿ios开关按钮
- android仿ios开关按钮
- 仿Ios滑动开关按钮
- android仿ios开关按钮
- 状态开关按钮(ToggleButton)与 开关(Switch)的功能与用法
- 第13章:状态开关按钮(ToggleButton)和开关(Switch)的功能和用法
- 状态开关按钮(ToggleButton)和开关(Switch)的功能与用法
- 状态开关按钮(ToggleButton)和开关(Switch)的功能与用法
- 开关按钮(ToggleButton)与开关(Switch)的功能与用法
- 自定义控件——开关按钮ToggleButton
- android基础笔记——自定义控件和视图:放ios系统开关按钮
- ios基础控件之开关按钮(UISwitch)
- MongoDB基础入门视频教程
- java interface 接口
- 九度OJ-1208-10进制VS二进制
- html5学习渐阶笔记---列表
- java 继承 覆盖
- IOS——重写按钮(开关功能)
- Going through codes with Nick
- skip a transaction in goldengate(跳过一个事务OGG)
- Python中cProfile性能分析工具的使用
- 谷歌域名大全
- 黑马程序员——多线程(一)
- 异步模式 Asynchronous models and patterns
- 自定义开关总结
- 网站压力测试工具webbench简介、安装、使用