[英]Swift: UIButton vs UIView
In Swift, it appears as though similar behavior can be achieved from a UIButton
and a UIView
. 在Swift中,似乎可以从
UIButton
和UIView
实现类似的行为。 To give functionality to a button, addTarget
can be called, whereas addGestureRecognizer
can be called for a UIView
. 为了让功能按钮,
addTarget
可以调用,而addGestureRecognizer
可以呼吁UIView
。 When should one be used over another? 应该何时使用另一个? In particular, when images are involved, is it better to use a
UIButton
paired with setImage
or UIImageView
paired with my_image_view.image = UIImage(...)
? 特别是,当涉及图像时,最好使用与
setImage
配对的UIButton
或与my_image_view.image = UIImage(...)
配对的UIImageView
吗?
Additionally, what does a UIButton
offer that a UIView
doesn't (and vice versa)? 另外,
UIButton
提供的UIView
不提供什么(反之亦然)? Thanks! 谢谢!
The choice is up to you but UIButton
is made for if you want a view to be touchable. 选择取决于您,但如果您希望视图可触摸,则可以使用
UIButton
。 Moreover, the UIButton
class includes every visual effects needed on a button (on press for example). 此外,
UIButton
类包括按钮上所需的每个视觉效果(例如,按下)。 It also has some attribute as selected
which can be useful for some uses. 它还具有一些
selected
属性,可用于某些用途。
To sum up, if the primary goal of your view is to be clickable, then you should use UIButton
. 总而言之,如果您的视图的主要目标是可点击,那么您应该使用
UIButton
。
If the thing you want is a thing you tap and UIButton can be made to work, use it. 如果你想要的东西是你点击的东西,UIButton可以使用,使用它。 That way the default accessibility is just correct (for vision impaired users).
这样,默认的可访问性就是正确的(对于视力受损的用户)。
If you need features of a UIImageView, but also want to be able to tap it, then use that and add a gesture. 如果你需要UIImageView的功能,但也希望能够点击它,那么使用它并添加一个手势。 You should also consider using the accessibility features to make it possible for vision impaired users to know that you intend the image to be tappable.
您还应该考虑使用辅助功能,使视障用户能够知道您打算使图像可以被点击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.