CAShapeLayer Line drawing - iphone

I am drawing line over 5 custom uiviews (Like UITableView rows) from one position to another (X,Y axis) using CAShapeLayer.
My issue is that I want to know that what view the CAShapeLayer (line in my case) is currently in. Is there any CGIntersect for CALayer and CGRect etc? I am trying to create graph using core animation instead of any chart API.

You could check in the shape layer intersects a specific layer by checking intersection of the bounding box of the path agains the frame of the other layer.
BOOL doesIntersect = CGRectIntersectsRect(CGPathGetBoundingBox(path), layer.frame);

Related

How to create round shape UIview instead of rectangle shape

Hi in my application i need a view which is of round shape instead of a rectangle shape. How to create a uiview object of round shape please let me know. Thanks in advance.
Technically, all UIView's will always be "rectangles", meaning they will be placed on the screen using {x, y} coordinates and they will be dimensioned with a height and a width (Making them rectangles). However, within the bounds of a UIView you can do a lot to make it appear as a circle. Here are some methods:
Use UIImageView and set it's Image to be an image of a circle. This is easy, but not very flexible.
Learn Core Graphics (also known as Quartz2D) and draw a circle in the UIView's -drawRect: method. Quartz 2D Programming Guide
Use a CAShapeLayer for the UIView's layers. CAShapeLayer Class Reference
There are certainly other methods but this should be a good start. If you need to detect touches within the circle, you can use either option 2. or 3. and keep a reference to the CGPathRef (or UIBezierPath) and use CGPathContainsPoint to determine if the touch is within the bounds of the circle and act accordingly.
You can set the cornerRadius of the layer of your view.
#import <QuartzCore/QuartzCore.h>
yourView.layer.cornerRadius = 20;

how to apply an imageview frame with the inclined coordinates

hi all upto now i know making rectangle with the CGrectmake and this rect(frame) i am using as imageview frame like UIImageView *someImage=[[uiimageview alloc]initwithframe:someRect]; now i can add an image with the frame of someRect. my problem here is when the coordinates like
(rectangleFirstx-coordinate,tectangleFirstY-cordinate)=(10,10)
(rectangleLastx-cordinate,rectangleLasty-cordinate)=(17,7) this, how can i give frame to the uiimageview....This is like a inclined rectangle..can any one suggest me how to apply frame through the ios library for these type of coordinates..Thanks in advance..
Your example isn't very clear because a rectangle with opposite corners at (10,10) and (10,7) can be in any one of a myriad of different orientations, including one perfectly aligned along the x and y axis.
What you can certainly do is create a UIImageView of the desired size and location and then rotate it by using one of many techniques, including animation methods.
[UIImageView animateWithDuration:0.1 animations:^
{
your_UIImageView_here.transform = CGAffineTransformMakeRotation((M_PI/180.0) * degrees);
}];
You can hide the UIImageView until the rotation is done and then show it.
If your question is about how to use the coordinates you provided to arrive at an angle I'd suggest that more data is needed because it is impossible to pick one of the billions of possible rectangles with corners at those two points without more information. Once you have more data then it is pretty basic trigonometry to figure out the angle to feed into the rotation.

is there MouseChildren for CALayer hit test?

Hey.
i have a main layer that contains a 4 circle layers and inside each circle there is a text layer :
main layer -> (4)circle layer -> (1)textLayer.
I am performing hit test on the main layer and i want to receive the circle that was clicked.
It works fine, but when I tap in the text area i get back the text layer and not the circle layer.
In AS3 you have
MouseChildren = true/false.
How can i get this functionality in objective c?
thanks
shani
Just use the superlayer property, like this:
if([theLayer isKindOfClass:[CATextLayer class]])
theLayer = theLayer.superlayer;
In other words, if you've already got the circle layer, theLayer doesn't change; if it's one of the text sublayers, theLayer changes to point to the layer that contains that sublayer.

Drawing CAShapeLayer with a big CGPath

I am creating a vector based drawing app, and being truly vector based I need to have a "limitless" canvas. So I have created the following structure:
UIVIew (canvas view)
|
CALayer (canvas container)
|
CAShapeLayer (the drawn shapes)
...
Each shape that has been drawn by the user is a CAShapeLayer with a CGPath assigned to it.
When the user zooms out the container (say the container has a size of about 10000x10000 points) and draws a new shape I get the points in scale of 1 and than assign a scale transform to the new CAShapeLayer (that`s the only way I found that works).
The problem is that in my app I can draw a connection between two objects, so I use a CAShapeLayer to draw the connection but when zoomed out (say the distance between two shapes is about 5000pts) I draw a line with two points which distance is about 5000pts as well. Doing that I think the springboard is restarted.
I've read that CAShapeLayer caches only the pixels of the drawn path and maybe that's my problem.
So I ask for your help guys because I ran out of ideas.
Is there a different approach to doing these huge shapes drawing and easily operate with them.
Thank you in advance.

Best way to flip a UIImage in a CALayer who's origin is not at (0,0)

Greetings... I come in peace, shoot to kill...
I have a container of type UIView (A Grid) and add many sublayers to the layer of the UIView (CALayers representing cells within the grid).
Within the Cell, I render many UIImages at different locations using CGContextDrawImage. I am well aware of the need to Translate and Scale, but the scaling (flipping) is with reference to the superviews (Grid) co-ordiantes and the origin of the Cell CALayer is not (0,0).
Therefore my rendering is all over the shop (mostly off screen). What is the best way to handle the translating and scaling when the UIImage is not at (0,0). Is there an established design pattern I should be using.
I solved this issue by just manually offsetting the translation by double the y origin.