CGAffineTrasformRotation also rotates the coordinating system - ios

I am rotating a UIView and then adding a new UIView to it. But it looks really weird.
Here is how it looks. The red box is the thing thats not as I expected.
I want the red box to be in the same direction as the blue box.
Im rotating the UIView like this:
[self setTransform:CGAffineTransformRotate(self.transform, DEGREES_TO_RADIANS(90))];
And then adding the red box as a UIView the normal way.
Im thinking that this is just the way transformations work and Im wondering if there is a way to fix it?

That's expected behavior if you're adding the red box after you perform the rotate. If you want the red box to have the same transformation as the blue one, then you'll need add the red box as its subview before applying the rotation transformation.

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.

Xcode colored square with text next to it. Legend like

I'm trying to create a legend that corresponds to colors on a map. No interaction. What I need is a colored square with text next to it. What I'm struggling with is how to go about creating this. I thought about programmatically creating each square but then I couldn't figure out how to set the text next to it. I then thought about a stack view with a view filled with a view colored and a label. Has anyone done something similar and if so how did you go about doing it?
You can create a square view with right background color, and than position your label next to it.

How to make a triangle on top of the UIView

How can a make a triangle on top of the UIView as a anchor to a UIButton, like in the picture below? I want the triangle to change the position when the other Button is selected. Please only answers written in swift. :)
Your screen shot displays a popover. Use a UIPopoverController and you'll get the triangle automatically.
Sometimes solutions are simpler then expected. Just draw all three triangles in storyboard. Then in your UIController you use the hidden flag to show or hide the triangle.

Fading CAShapeLayer path appearance

I have an issue with showing a CAShapeLayer onto a CALayer.
Here is what I try to achieve:
I have an overlay over my view, I make a hole in the overlay so I can reveal what's behind. This works fine.
Now I want to be able to animate the creation of the hole. I'd like to have the hole part fading from the overlay color to transparent.
I already tried drawing another shape over the hole and make it disappear, it works, but this is not perfect as you can see in the last image (I can make it less visible, but still, it is not enough), and I don't think this is the correct way to do it.
If you have any idea or advice!
Thanks!

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