简体   繁体   中英

How to get non-rectangular shaped button?

How can I get a non-rectangular shaped button?

I have a UIImage with a mask. How can I set the shape of a button to this image without the transparent color.

在此处输入图片说明

I have a UIImage with a mask. 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:) .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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