Swift - 按钮(UIButton)的用法
来源:互联网 发布:淘宝自动软件是真的吗 编辑:程序博客网 时间:2024/05/30 19:34
1,按钮的创建
(1)按钮有下面四种类型:(2)对于Custom定制类型按钮,代码可简化为:
2,按钮的文字设置
3,按钮文字颜色的设置
4,按钮文字阴影颜色的设置
5,按钮背景颜色设置
6,按钮文字图标的设置
7,设置按钮背景图片
8,按钮触摸点击事件响应 常用的触摸事件类型:
(1)按钮有下面四种类型:
UIButtonType.ContactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.DetailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.System:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.Custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
UIButtonType.InfoDark:为感叹号“!”圆形按钮
UIButtonType.InfoLight:为感叹号“!”圆形按钮
1
2
3
4
5
6
7
//创建一个ContactAdd类型的按钮
var
button:
UIButton
=
UIButton
.buttonWithType(
UIButtonType
.
ContactAdd
)
as
UIButton
;
//设置按钮位置和大小
button.frame=
CGRectMake
(10, 150, 100, 30);
//设置按钮文字
button.setTitle(
"按钮"
, forState:
UIControlState
.
Normal
)
self
.view.addSubview(button);
1
var
button =
UIButton
(frame:
CGRectMake
(10, 150, 100, 30))
1
2
3
button.setTitle(
"普通状态"
, forState:
UIControlState
.
Normal
)
//普通状态下的文字
button.setTitle(
"触摸状态"
, forState:
UIControlState
.
Highlighted
)
//触摸状态下的文字
button.setTitle(
"禁用状态"
, forState:
UIControlState
.
Disabled
)
//禁用状态下的文字
1
2
3
button.setTitleColor(
UIColor
.blackColor(),forState: .
Normal
)
//普通状态下文字的颜色
button.setTitleColor(
UIColor
.greenColor(),forState: .
Highlighted
)
//触摸状态下文字的颜色
button.setTitleColor(
UIColor
.grayColor(),forState: .
Disabled
)
//禁用状态下文字的颜色
1
2
3
button.setTitleShadowColor(
UIColor
.greenColor(),forState:.
Normal
)
//普通状态下文字阴影的颜色
button.setTitleShadowColor(
UIColor
.yellowColor(),forState:.
Highlighted
)
//普通状态下文字阴影的颜色
button.setTitleShadowColor(
UIColor
.grayColor(),forState:.
Disabled
)
//普通状态下文字阴影的颜色
1
button.backgroundColor=
UIColor
.blackColor()
1
2
3
button.setImage(
UIImage
(named:
"icon1"
),forState:.
Normal
)
//设置图标
button.adjustsImageWhenHighlighted=
false
//使触摸模式下按钮也不会变暗
button.adjustsImageWhenDisabled=
false
//使禁用模式下按钮也不会变暗
1
button.setBackgroundImage(
UIImage
(named:
"background1"
),forState:.
Normal
)
1
2
3
4
5
6
7
8
9
10
11
//不传递触摸对象(即点击的按钮)
button.addTarget(
self
,action:
Selector
(
"tapped"
),forControlEvents:
UIControlEvents
.
TouchUpInside
)
func
tapped(){
println
(
"tapped"
)
}
//传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号
button.addTarget(
self
,action:
Selector
(
"tapped:"
),forControlEvents:
UIControlEvents
.
TouchUpInside
)
func
tapped(button:
UIButton
){
println
(button.titleForState(.
Normal
))
}
TouchDown:单点触摸按下事件,点触屏幕
TouchDownRepeat:多点触摸按下事件,点触计数大于1,按下第2、3或第4根手指的时候
TouchDragInside:触摸在控件内拖动时
TouchDragOutside:触摸在控件外拖动时
TouchDragEnter:触摸从控件之外拖动到内部时
TouchDragExit:触摸从控件内部拖动到外部时
TouchUpInside:在控件之内触摸并抬起事件
TouchUpOutside:在控件之外触摸抬起事件
TouchCancel:触摸取消事件,即一次触摸因为放上太多手指而被取消,或者电话打断
转自:http://www.hangge.com/blog/cache/detail_529.html
0 0
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- iOS 【Swift - 按钮(UIButton)的用法】
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- swift 3.0 按钮的用法(UIButton)
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- swift-按钮(UIButton)用法
- Swift开发入门:按钮(UIButton)空间
- Swift-按钮(UIButton)详解
- Swift 按钮(UIButton)
- vc++导入com组件
- Codeforces Round #320 (Div. 2) A B C D E
- JNI 常用API
- 关于引用及取地址&运算符的用法
- 循环冗余校验(CRC)算法入门引导
- Swift - 按钮(UIButton)的用法
- MVP架构模式以winform为例
- ubuntu matlab快捷方式创建
- 关于jdbc重点讲解实例--使用jdbc操作数据库上传附件
- 10----时间格式的转换
- JavaBean的get、set方法引发的讨论
- Spring Resource bundle with ResourceBundleMessageSource example
- 指针
- pdf转换成html格式怎么转