How can i detect whether two CALayers overlap? - ios

In my app the user can drag the top CALayer from a stack of CALayers.
When the users stops dragging the CALayer should return to its original position on top of the others CALayers. Unless the CALayer (after dragging) no longer overlaps with the other CALayers in which case it should move underneath the other CALayers.
Now, how can I detect whether two CALayers overlap?

CALayer's frame property is a CGRect. Fortunately for you, CGGeometry has the following method:
CGRectIntersectsRect
You'd use it like this:
if (CGRectIntersectsRect(myLayer1.frame,myLayer2.frame)) {
//code
}

Related

Animating an image over another image in iOS

So basically i have two image objects, 1 portrays a line and the other a circle. I wanted to know if its possible using something like CABasicAnimation to loop the line image across the x axis but still keeping it within the circle image?
Yes, you should be able to do this with either CABasicAnimation or UIView animation.
CAAnimations only operate on CALayers, and they are rather tricky to use. I suggest using UIView animations on UIImageViews or other UIViews instead. You could even set up a custom subclass of UIView to have a CAShapeLayer as it's backing layer and then animate the custom UIView using UIVIew animations

CGRectIntersectsRect two frames from different view hierarchy

I have a UIButton and a UIView that belong to two different views hierarchies and I'm trying to detect a collision when I drag my button to the “viewTrashArea”.
The problem is that the frame of the button and the frame of the view are in different coordinates and therefore make the collision think that they are touching but they are far away from each others.
How can I detect the collision based on the global screen position?
Given
UIButton *button;
UIView *viewTrashArea;
This line would return true if they intersect:
CGRectIntersectsRect([button convertRect:button.bounds toView:viewTrashArea], viewTrashArea.bounds);
You'll need to convert the positions to a common coordinate system. Use the UIView methods convertRect:fromView: and convertRect:toView: for this purpose.

Best way to draw line between fix CGPoint and moving UIView object's center point

I have a UIView subclass object that animates and therefore changes its position over time as a subview in my UIViewController's view. Actually my moving UIView subclass is just an image of a ball and it's moving as if it was hanging down from my devices screens top border. But to be a real pendulum I'd like to also draw a line between my ball and the CGPoint it hangs down from on top of my screen.
My idea was to just draw a line every time the UIView changes its position. But as the moving is done within an iOS API (I'm just calling something like [myBallView swing]) I can't do this at the same place the movement is happening. I'm actually not animating the view myself.
The only solution I have in my mind to solve my issue is pretty bad: subclassing UIView, adding it as a superview to my moving UIView and adding a line every time drawRect is called. But I'm not even sure drawRect is going to be called there. Either way, there must be a better solution for this.
Does anyone know a good solution to my problem?
Making a custom subclass of UIView as the superview is reasonable.
However, rather than drawing the line yourself, I would suggest implementing +layerClass in your custom view, and making the backing layer a CAShapeLayer.
Then you can create a CGPath in the view's shape layer that is a line, and manipulate the start and end points of the line when your other view's center moves. Shape layers are designed to draw as part of the screen update process, and you could even make the change animate by changing the endpoints of the path with a CABasicAnimation.

UIView animation in drawrect

I need to continuously change the color of all the lines I draw in a UIView. And I drew all the lines within the drawrect method to assign colors. Now I need to animation and change the colors as long as the view is shown. Is there a way to do it? Thanks a lot!
Look into using one instance of CAShapeLayer for each of the lines, instead of drawing them in drawRect:. You can animate changes to each layer's path and strokeColor properties as necessary.
EDIT
If you're just drawing horizontal lines, you may be better off simply using a UIView for each. Set the backgroundColor instead of implementing drawRect:. You can animate both the frame property and the backgroundColor property using +[UIView animateWithDuration:animations:] (or the more powerful variants of it). This is generally simpler than messing around with Core Animation.
Live and learn. Animations are better done outside of -drawRect:
Change your "lines" to individual UIView instances added as (preferably opaque) subviews to your view. You'd then set the appropriate backgroundColor on the view.
You can then use a simple UIView animation to animate them.

Multiple UIView transformation animations, one repeats, the other does not

I have a UIImageView whose instance has a repeating pulse-like animation.
I want this UIImageView to move with the force of the accelerometer. To do this I have added another animation, this time a translation.
However, once this translation happens, it stops the pulse, even when its not animating.
As these two animations are separate, I cannot use the CGAffineTransformConcat method to combine both animations.
I have tried adding one to the UIView and one to the layer property of the same UIView but it does not work.
I have also tried adding the movement directly to the frame of the view, however, this just causes my view to disobey the constraint constraining it to the center x of the superview.
Does anyone use/know of a work around to this?
Thanks as always!

Resources