iPhone切换按钮实现 [英] iPhone toggle button implementation

查看:97
本文介绍了iPhone切换按钮实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iPhone应用程序中创建一个切换按钮。但是,我不知道究竟什么是最好的方法。

I would like to create a toggle button in my iPhone application. However, I don't know exactly what would be the best approach for this.

我正在考虑两种选择。

我可以继承UIButton。这样我就不必实现触摸处理了。我可以创建一个方法,返回一个布尔值,指示按钮是处于打开还是关闭状态。

I could subclass an UIButton. That way I do not have to implement touch handling. I could just create a method that returns a boolean indicating whether the button is in on or off state.

我也可以从UIView子类开始并从头创建它。我认为像转换动画这样的事情会更容易,但我必须创建自己的触摸处理。

I could also start with an UIView subclass and create it from scratch. I suppose it would be easier to do things like a transition animation, but I would have to create my own touch handling.

您认为最好的是什么?或者你会知道更好的方法吗? (也许网上有开源/免费视图?)

What do you think is the best one? Or would you know a better way? (Maybe there is an open-source/free view available on the web?)

编辑:我不想使用UISwitch因为我希望我的切换按钮看起来像那样。我希望它是一个大的矩形按钮,并改变背景以指示状态。

I don't want to use the UISwitch because I want my toggle button to look like that. I want it to be a big rectangular button and make the background change to indicate the state.

谢谢!

推荐答案

您可以使用 UIButton 并设置其已启用属性。

You can use UIButton and set its enabled property.

在Interface Builder中,您可以为启用和禁用状态设置单独的图像,并且 IBAction 更改属性。

In Interface Builder, you can set separate images for the enabled and disabled states, and the IBAction change the property.

这篇关于iPhone切换按钮实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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