简体   繁体   English

如何获得非矩形按钮?

[英]How to get non-rectangular shaped button?

How can I get a non-rectangular shaped button? 如何获得非矩形的按钮?

I have a UIImage with a mask. 我有一个带蒙版的UIImage How can I set the shape of a button to this image without the transparent color. 如何在没有透明颜色的情况下对此图像设置按钮的形状。

在此处输入图片说明

I have a UIImage with a mask. 我有一个带蒙版的UIImage。 How can I set the shape of a button to this image without the transparent color. 如何在没有透明颜色的情况下对此图像设置按钮的形状。

Buttons are controls, and controls are views, and views are inherently rectangular, so a button will always occupy a rectangular space in the view hierarchy. 按钮是控件,控件是视图,视图本质上是矩形,因此按钮在视图层次结构中将始终占据矩形空间。 However, the visible part of a view (and therefore a button) can be whatever you want it to be... a view can have a transparent background, and can draw itself however it likes. 但是,视图的可见部分(以及按钮)可以是您想要的任何内容...视图可以具有透明的背景,并且可以根据自己的喜好进行绘制。 A view can also choose to pretend that a touch event doesn't hit it, potentially making the view seem to have a non-rectangular shape for the purpose of delivering touches. 视图也可以选择假装没有发生触摸事件,从而有可能使视图看起来具有非矩形的形状,以实现触摸的目的。 You can do that by overriding hitTest(_:with:) . 您可以通过覆盖hitTest(_:with:)来做到这一点。

Also, realize that you don't always need to use buttons in order to interact with objects on the screen. 另外,请意识到您并不需​​要总是使用按钮来与屏幕上的对象进行交互。 If you have an image of a house, for example, and you want the user to be able to tap different parts of the house to change its color or texture, you could display the house in a view that knows where different parts of the house are in the image. 例如,如果您拥有房屋的图像,并且希望用户能够点按房屋的不同部分以更改其颜色或纹理,则可以在知道房屋不同部分位置的视图中显示房屋在图像中。 You could use gesture recognizers or the normal touch handling mechanism to let the user interact with the different regions, and those regions can be any shape you like. 您可以使用手势识别器或常规的触摸处理机制来让用户与不同区域进行交互,并且这些区域可以是您喜欢的任何形状。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM