斯威夫特的UIButton上OnTouchUp和OnTouchDown背景的变化? [英] Swift UIButton background change on OnTouchUp and OnTouchDown?

查看:390
本文介绍了斯威夫特的UIButton上OnTouchUp和OnTouchDown背景的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的迅速和我的应用程序有一个UIButton,为此,我想改变背景颜色和边框颜色上的键,和关键的。

I am using swift and in my app there is a UIButton, for which I want to change background color and border color on key down and key up.

推荐答案

您可以只创建两个函数,并把它们连接到相应的事件。像这样的:

You can just create two functions and connect them to appropriate events. Like this:

@IBAction func up(){
    myButton.backgroundColor = UIColor.whiteColor()
}

@IBAction func down(){
    myButton.backgroundColor = UIColor.blackColor()
}

现在只是将它们连接接口生成器。功能上()连接到触摸的内心和润色之外。功能向下()来降落。

Now just connect them in interface builder. Function up() connect to Touch Up Inside and Touch Up Outside. Function down() to Touch Down.

但theres一个更好的方法,把你的按钮,并在code设置他的背景图像的状态。

But theres is a better way, take your button and in code set his background image for state.

//this will show when button is released
myButton.setBackgroundImage(UIImage(named:"background_image_normal"), forState:UIControlState.Normal)

//this will show when button is pressed
myButton.setBackgroundImage(UIImage(named:"background_image_highlighted"), forState:UIControlState.Highlighted)

这篇关于斯威夫特的UIButton上OnTouchUp和OnTouchDown背景的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆