I managed to draw this circle with segments using core graphics.
The circle is drawn using CGContextFillEllipseInRect and I added lines on top of it using CGContextStrokePath.
Is it possible to add interactivity to this circle segments to identify which segment area was touched by the user?
How Can I change the behaviour of each segments in the circle, for example Color, image & title for each segments?
If you know the centre of the circle and you know the touch position it is fairly easy to calculate the angle that the touch is at.
Then it's just a case of separating the angles into different sections and reacting to the touch accordingly.
Related
I have a list view with "Hero" icons on the left. When I click a list item, it loads the next screen with the article text and the Hero image (which animates nicely/automatically to the correct spot on the 2nd screen).
I would have thought that was the "tough" part, but I'm now trying to get a curved shape as the top background of the 2nd screen. I would love to make it a drawn vector shape, as opposed to a bitmap and even have it drip/bounce onto the page, but at the moment...
I'm just trying to figure out how to:
draw a vector shape
have it as the background of a screen with other widgets on top (see purple curve on 2nd screen below)
I made a full sample for your curved shape in a gist here
I used CustomPainter to draw on a canvas then, with some geometric calculations, I achieved the curved shape.
Final result
How I draw it?
Before coding and on a Whiteboard I determined somethings:
My Canvas Area:
The canvas dimensions I need to draw that shape (which equals to Flutter widget's dimensions).
How and where my brush will move?
how means: what are the APIs I need to draw that shape on the canvas using the Path class.
e.g. lineTo() for a straight line, quadraticBezierTo() for a curve.
where means: Where are the points (coordinates) I need to draw the whole shape. (see yellow and green dots in the image above)
Points (coordinates) Calculations:
I used some geometric equations to calculate the coordinates. e.g. Point on a circle’s circumference
All of my calculations depend on the canvas size, that gives me a responsive shape.
Full sample here!
Hi I am trying to implement a polygon with a color gradient based on the color of each corner (the polygon is an overlay for a map). Up until now I have been using MKPolygon. Any Guidance will be appreciated.
Thanks,
To Achieve my result I wrote a for loop that iterated through all the points, which had a for loop inside that subtracted a value from the other points to make the polygon smaller and created a polygon this was done until the polygon was small enough for the view. This gave it a nice gradient and doing it for the other cornea with different colors gave the desired gradient of colors.
I'm trying to change the shape of a CAShapeLayer from a circle to a different shape. Looking at this question:
Smooth shape shift animation
I found the solution but my question is how can I visually see how many points a UIBezierPath has. Is there a way to color a point different than the line it produces?
For example,it's simple with a line to understand that there are two point, but if we make a circle with bezierPathWithRoundedRect, does that count as one point or are there more?
You would need to add the circle yourself to visually see the dots. Just keep track of the CGPoints you give to the path and draw a circle around each one.
Btw.. if you use PaintCode, you can edit the bezier path and see the point -- it's very useful.
I have two shapes in a UIView - one, an ellipse and two, a triangle drawn using UIBezierPath. I need to draw the outline of these two shapes combined. How can I do it?
You can do an "outside" stroke (like stroke->outside in photoshop/pixelmator) by calling stroke to draw the outline and setting the inverse of your shapes as the clipping path first. To do the inverse of the clipping path see this answer: https://stackoverflow.com/a/10639523/461492 (read comments too).
So here are the steps:
Set the full area as the clipping path.
Call CGContextEOClip() for each of your shapes as described in the comments to the answer linked above.
Stroke your shapes.
This might not be exactly what you want - it will draw the stroke as normal but the whole interior (the fill area) of your shapes will not be drawn. So whereas the thickness of the stroke would normally extend within the interior of your shapes, and the internal angles of your stroke would normally have the correct corners (rounded/mitered) - in this case it would be more like you stroked the shapes then deleted the fill-area, or did an "outside" stroke in an image editing program.
I need to animate arcs (a.k.a donut segments) in the following scenarios where the arc maintains a constant radius r to the imaginary circle center (the arc sits right outside the circle).
1) Animate the arc stroke width from x to y, while maintaining a radius r and angle alpha.
2) Animate the arc angle from alpha to beta while maintaining a constant stroke width and radius.
3) do 1 and 2 together but possibly with independent animations/timings.
Here's what I have so far:
I’ve implemented the arc drawing as a custom view that simply draws the arc with CGContextAddArc. This is fine for a static arc but it doesn’t animate anything.
Also, I know how to draw clipped images with things like [UIBezierPath addClip].
The latter is interesting because I think that for scenario 1, I can achieve the desired effect in two ways: either keep drawing an arc and modify both stroke and radius to maintain the same perceived inner circle radius (which I’m not optimistic about, I’m afraid that the radius will “jiggle”), or draw a segment of a circle that grows in size (maybe by simply modifying the scale with an affine transform) and is then clipped by a static circular mask.
Now, how do I take all these concepts and nail them down into some actual drawing code? I don’t need real code (though that would be fine too), but more like a conceptual approach, like, can I do this all with a UIView with custom drawing, or do we need to talk about custom key animations that I understand involve CALayers and such. In other words, what’s the right architecture to do all this that would be easiest to code while being efficient from a compositing perspective for smooth animation?
You can already do this with a CAShapeLayer by creating the path for the arc and then animate different stroke properties. You could create the path for the full circle and use the strokeStart and strokeEnd properties to only stroke a certain part of the circle. It is worth noting that the shape layer is center stroked so they increase equally inwards and outwards as you increase the line width. To counter this you could either mask it with the same circle shape and double the line width or animate the path so that the radius increases by half of the line width increase so that the inner most point has the same distance to the center at all times.
The first example can be done by animating the lineWidth property and the second can be done by animating the strokeStart and strokeEnd properties
You should implement this using custom animatable properties on a CALayer subclass. This tutorial (with source here) is for creating animated pie charts and looks pretty good. You should be able to modify it for your requirements.