How to get non-rectangular shaped button? - ios

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.

Related

UIViewController transparent background colour

I'm trying to achieve the following behavioural attached in the image and I did it using vies with custom classes, the question is that how can I achieve this using view controllers,
the behavioural works as follow: when tapped and swipe left the previous screen should start appear but without pop from navStack, just slide to show and when user leave his finger will return to initial state which same as above image, I did it by adding a UIGestureRecognizer on this white slide edge start change the x position for the upper view but, both upper and previous are views not UIViewColtrollers, the question is how to implement this using UIView controllers or make the background colour of the upper view controller transition colour.
have any idea about this or do sth like this before to help ?

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.

UIButton custom image alpha

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.

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.

IOS - How to draw a border and a shadow that rotates with view but doesn't scale with it

I am developing an ios app.
In my app I have got a view, the view is has got a border and a shadow.
The view can be scaled and rotated.
When a user scaled my view it causes my border and shadow to scale as well.
I don't want my shadow or border to scale at all.
On the other hand I do want my shadow and border to rotate when I rotate the view.
Is there any built in mechanism to prevent border \ shadow scale?
Thank you
As far as I know there isn't such a built in mechanism.
The only way I see for you to solve your problem is to simply use two different views, with different management in case of rotation.
Have one view as a sort of background and add your border and shadow to it, and then place the content on top of it (or in it, depends on your case).

Resources