Two UIViews overlaped - ios

I have two UIViews, each one has draw a car(vectorgraph), now if a car(A) behind another car(B), then I want the overlaps in A be dashed.
the car was drawn by UIBezierPath, I want the overlaps in A's path become dashed, how can I do this?
Thank you for your helping!

I don't think there is any way to do this automatically. You would need to calculate the bounding rectangle of the intersection of the 2 views, and then manually parse the path of car A into 2 parts, the part that's covered and the part that's not, and draw the covered part using dashed lines.
This is original development that you would have to do yourself.
Erica Sadun's outstanding "iOS Developer's Cookbook" series has a recipe that shows how to parse a bezier path segment by segment. Figuring out the portion of the path that is inside the intersection range will involve some tricky programming.

Related

Draw text along a path on a map (like f.e. a street-name)

I am currently trying to label lines that I draw in my Map (in my iOS app, but I guess it applies to all maps).
So what I currently am doing, I simplify my path so that I get rid of most small curves and then just draw my glyphs along that line. Currently that looks like this:
On some parts of the line that's already ok. If the line is quite straight and the corners aren't too spiky.
But in some parts you can just not read anything... So what are strategies to make that look nicer?
Does anybody know an algorithm or a strategy on how to make my path look like the red line here:
I am happy about any ideas on how to improve my drawing :)
I do it, in my commercial map rendering system, by finding a portion of the line without sharp corners. There is no way to make the label look good if it turns corners of a right angle or greater. If there's no section long enough I abbreviate the label (e.g., Link Road becomes Link Rd), or split it on to two lines. If there's still nowhere to draw the label I don't draw it.
Another thing that's important is to adjust the spacing so that ascenders and descenders don't clash, so you need to look at the bounding box of each adjacent pair of letters as you draw the text and add small amounts of space as necessary.
I don't bother to smooth my lines, as you suggest with your red line. It really doesn't seem to matter, at least with street labelling.

Interact with complex figure in iOS

I need to be able to interact with a representation of a cilinder that has many different parts in it. When the users taps over on of the small rectangles, I need to display a popover related to the specific piece (form).
The next image demonstrates a realistic 3d approach. But, I repeat, I need to solve the problem, the 3d is NOT required (would be really cool though). A representation that complies the functional needs will suffice.
The info about the parts to make the drawing comes from an API (size, position, etc)
I dont need it to be realistic really. The simplest aproximation would be to show a cilinder in a 2d representation, like a rectangle made out of interactable small rectangles.
So, as I mentioned, I think there are (as I see it) two opposite approaches: Realistic or Simplified
Is there a way to achieve a nice solution in the middle? What libraries, components, frameworks that I should look into?
My research has led me to SceneKit, but I still dont know if I will be able to interact with it. Interaction is a very important part as I need to display a popover when the user taps on any small rectangle over the cylinder.
Thanks
You don't need any special frameworks to achieve an interaction like this. This effect can be achieved with standard UIKit and UIView and a little trigonometry. You can actually draw exactly your example image using 2D math and drawing. My answer is not an exact formula but involves thinking about how the shapes are defined and break the problem down into manageable steps.
A cylinder can be defined by two offset circles representing the end pieces, connected at their radii. I will use an orthographic projection meaning the cylinder doesn't appear smaller as the depth extends into the background (but you could adapt to perspective if needed). You could draw this with CoreGraphics in a UIView drawRect.
A square slice represents an angle piece of the circle, offset by an amount smaller than the length of the cylinder, but in the same direction, as in the following diagram (sorry for imprecise drawing).
This square slice you are interested in is the area outlined in solid red, outside the radius of the first circle, and inside the radius of the imaginary second circle (which is just offset from the first circle by whatever length you want the slice).
To draw this area you simply need to draw a path of the outline of each arc and connect the endpoints.
To check if a touch is inside one of these square slices:
Check if the touch point is between angle a from the origin at a.
Check if the touch point is outside the radius of the inside circle.
Check if the touch point is inside the radius of the outside circle. (Note what this means if the circles are more than a radius apart.)
To find a point to display the popover you could average the end points on the slice or find the middle angle between the two edges and offset by half the distance.
Theoretically, doing this in Scene Kit with either SpriteKit or UIKit Popovers is ideal.
However Scene Kit (and Sprite Kit) seem to be in a state of flux wherein nobody from Apple is communicating with users about the raft of issues folks are currently having with both. From relatively stable and performant Sprite Kit in iOS 8.4 to a lot of lost performance in iOS 9 seems common. Scene Kit simply doesn't seem finished, and the documentation and community are both nearly non-existent as a result.
That being said... the theory is this:
Material IDs are what's used in traditional 3D apps to define areas of an object that have different materials. Somehow these Material IDs are called "elements" in SceneKit. I haven't been able to find much more about this.
It should be possible to detect the "element" that's underneath a touch on an object, and respond accordingly. You should even be able to change the state/nature of the material on that element to indicate it's the currently selected.
When wanting a smooth, well rounded cylinder as per your example, start with a cylinder that's made of only enough segments to describe/define the material IDs you need for your "rectangular" sections to be touched.
Later you can add a smoothing operation to the cylinder to make it round, and all the extra smoothing geometry in each quadrant of unique material ID should be responsive, regardless of how you add this extra detail to smooth the presentation of the cylinder.
Idea for the "Simplified" version:
if this representation is okey, you can use a UICollectionView.
Each cell can have a defined size thanks to
collectionView:layout:sizeForItemAtIndexPath:
Then each cell of the collection could be a small rectangle representing a
touchable part of the cylinder.
and using
collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
To get the touch.
This will help you to display the popover at the right place:
CGRect rect = [collectionView layoutAttributesForItemAtIndexPath:indexPath].frame;
Finally, you can choose the appropriate popover (if the app has to work on iPhone) here:
https://www.cocoacontrols.com/search?q=popover
Not perfect, but i think this is efficient!
Yes, SceneKit.
When user perform a touch event, that mean you knew the 2D coordinate on screen, so your only decision is to popover a view or not, even a 3D model is not exist.
First, we can logically split the requirement into two pieces, determine the touching segment, showing right "color" in each segment.
I think the use of 3D model is to determine which piece of data to show in your case if I don't get you wrong. In that case, the SCNView's hit test method will do most of work for you. What you should do is to perform a hit test, take out the hit node and the hit's local 3D coordinate of this node, you can then calculate which segment is hit by this touch and do the decision.
Now how to draw the surface of the cylinder would be the only left question, right? There are various ways to do, for example simply paint each image you need and programmatically and attach it to the cylinder's material or have your image files on disk and use as material for the cylinder ...
I think the problem would be basically solved.

How to get common area of two nodes in SpriteKit?

First of all, what I want to achieve is a shape node filled with two different, distinct colors (not blended), e.g. top part of a circle is white, bottom is black.
From what I've found, my only option is to manually create paths and color them. That's fine with me, but I first need to know the exact paths.
I'm trying to avoid complex calculations, so my question is - is there any way to get a path describing a common area of two overlapping nodes?
A simple example to illustrate the problem: when the circle collides with the black area, the part of the circle which overlaps black rectangle should change colour - how can I get that overlap area's path?

Drawing a line with hidden parts in cocos2d for ios

Say you have an array of points and an array of rects, and you want to do the following:
Draw a line that connects all the points in the point array, such that the parts of the line that are contained in at least one of the rects are not visible.
What is the best practice way to accomplish such a task in cocos2d ios?
This may sound like a hack, but if you are drawing the rectangles as well then you can draw them with a higher zOrder with respect to the line and the rectangles would could cover the parts of the lines they contain making those parts invisible.
Hope it helps!

Drawing a non rectangular part of a picture in delphi canvas

Can anyone share a sample code to draw a non-rectangular part of a picture in delphi canvas?
You're looking for GDI paths. Start here, which explains what paths are in this context, and provides links on the left to explain the functionality available with them.
Google can turn up lots of examples of using paths in Delphi. If you can't find them, post a comment back here and I'll see what I can turn up for you.
Your question is pretty vague. But I suspect what you are looking for is clipping regions. Read up on them. Set the clipping region on the target device to the shape you want, and then draw the image onto the device. Only the part of the image that would be within the clipping region will be drawn.
Canvas.Ellipse(0, 0, 10, 20); // not a rectangle
I use so called runlists for this feature (generalized shapes and blitting them). I've seen them called warplists too. A shape is encoded as a runlist by defining it as a set of horizontal lines, and each line is two integer values (skip n pixels,copy n pixels).
This means you can draw entire lines, leaving you with only "height" draw operations.
So a rectangle is defined (the first "skip" pixels from top level corner to the left corner (xorg,yorg). The rectangle is width_rect wide, and width_pixels goes a line further. width_pixels can be wider than the width of the picture (alignment bytes)
(yorg*width_pixels+xorg , width_rect),
(width_pixels-width_rect , width_rect),
(width_pixels-width_rect , width_rect),
(width_pixels-width_rect , width_rect),
..
..
This way you can make your drawing routines pretty generic, and for simple, regular shapes (rects, circles) it takes only minor math to precalculate these lists. It simplified my shape handling enormously.
However I draw directly to bitmaps, not to canvasses, so I can't help with that part. A primitive that efficiently draws a row, and a way to extract a row from a graphic should be enough.

Resources