UIButton custom image alpha - ios

I have some buttons close to each other in a logo shape, they are non rectangular shapes and I was wondering if I can exclude the transparent parts of the button from receiving touches, so that the elements behind it can receive the touches instead?.
Can you make the alpha parts of the button image not part of the button? , or , if the user touches these alpha parts, the button below will be tapped?

You would normally have to subclass UIButton, but luckily someone has taken care of this for you already:
https://github.com/ole/OBShapedButton
This seems to be exactly what you're looking for.

Related

Custom Drawn UIButton

I need to have a circular UIButton that draws a custom color in the middle, along with an outer ring that is drawn as the default tint color (similar to a UIColorWell).
I've tried a few different approaches:
Using a multicolor SFSymbol: This would be an elegant solution, but as far as I can tell there's no way to apply the tint color to just a part of the image while setting the center to be a custom color. Either the entire image is tinted, or the image is drawn as the default colors set in the symbol file. Also, I need to support iOS 14, while the new hierarchical options that may allow me to accomplish this were added to iOS 15.
Setting various layer properties (ie, cornerRadius, borderColor, etc): This works and may be a decent fallback solution, but I'm unable to get the look that I'm going for (namely, having a transparent ring between the outer border and inner colored circle).
If there's a way to use either of the above options, please educate me! Either one seems like a better solution than:
Overloading the draw function: This is the option I'm going with at the moment, as it allows me to have complete creative control over the look of the button. The rest of this post will be regarding this method.
So far I was able to get the button to be drawn exactly as I wanted. However, I am unable to figure out how to draw the button appropriately with regards to various state changes.
For example, if a popover is displayed, all of the normal buttons are automatically redrawn as disabled. My custom button, however, isn't redrawn so I am unable to respond to the state change.
Same thing with tapping on the button - normal buttons are briefly shown in an emphasized color but my custom button doesn't respond.
Does someone have an example as to how to support overriding UIButton drawing with various states?
I was able to get the desired behavior by overriding tintColorDidChange in order to trigger a redraw of the button. Now I am able to draw the outer ring in the correct color (including grayed out when a popover is displayed) while maintaining the desired inner color.

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.

Make UIButton shine on touch

I have a set of invisible UIButtons on the screen. When I say invisible I mean set to 'custom' without an image. I would like to make so that when the button was pressed it glowed for 1 second, so that the user can see he has pressed a button. How can I do this?
Use a blank image for UIControlStateNormal. Make an image for UIControlStateHighlighted which is your glow.
Rethink this design, because it doesn’t sound like it will fit well with the direction iOS 7 is going.

Create UIView with custom shape

So I was wondering if there's a way to make a UIView with a custom shape.
I'm trying to make a piano keyboard so when the user touches the view a delegate method responds by playing a noise. The picture is a .png with the picture of the key and a transparent background.
Thanks in advance.
You can't make a UIView in the shape of, say, a triangle, but what you can do is make the view transparent, then add your non-transparent content to it.
For example, to make a view with a background of a png (with, I assume, partially transparent areas) you could make a transparent UIView and then add an image view to it. better yet, just use a UIImageView in the place.

how to use different shape of UIButton

I am having below image for UIButton background image.
But UIbutton shape by default is round rect.So touch up inside event will work only in round rect area.
So how can i support touchup inside event for below shape (entire area).
Hope this link helps.
To create an irregular shaped button, you need to create custom button and perform hitTest to check if button was pressed or empty space was pressed.
You can use UIButtonTypeCustom and give the correct frame.

Resources