I am trying to use a UIBezier path for drawing a line using user touch. I have the physics body and bezier working separately although I need to merge them together. Is there a way to implement drawRect or an equivalent within SpriteKit? Thanks in advance!
Nope. DrawRect or other custom drawing methods are not (currently) supported by Sprite Kit. Use SKShapeNode to draw the path.
SKShapeNode* shape = [SKShapeNode node];
CGMutablePathRef path = CGPathCreateMutable();
// create your path here ...
shape.path = path;
[self addChild:shape];
Related
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;
I have a layer that will move from point A to point B on a UIBezierPath.
I have found a lot of samples those are refers to CAAnimation and UIBezierPath.
But I need to move my layer only from specified point to another on bezier path.
Any suggestions would be appreciated.
Thanks
Hope this will be helpful.
UIBezierPath *trackPath = [UIBezierPath bezierPath];
.
.
.
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:#"position"];
anim.path = trackPath.CGPath;
anim.repeatCount = 1;
anim.duration = 2.0;
[layerToAnimate addAnimation:anim forKey:#"pathGuide"];
If your bezier path is a circle or even a half circle, you could just skip the path and instead add the point to a larger square layer. Set the point to be a fixed distance from the center of that larger square layer. Then you can just rotate the larger layer on the z axis around the center whatever number of degrees you need it to move. Seems it would simplify things a bit, though I'm not sure exactly what else you need it to do.
i'd been looking to do something like this and fount this tutorial, it shows how to follow a specific path. It works with a car (a CALayer) and a UIBezierpath as the race track and is solved in this order:
Defining the path the car should follow (in this case your BezierPath)
Drawing the black line that defines the track; (N/A)
Drawing the white dashed center-line of the track; (N/A)
Creating the layer defining the car; (your Layer)
Animating the car along the path. (What your asked!)
You can check the reference Post here:
http://nachbaur.com/2011/01/07/core-animation-part-4/
Also you can download the source code here:
http://cdn5.nachbaur.com/wp-content/uploads/2011/01/CALayerAnimTest.zip?25881d
hope this helps!
regards,
Jorge.
I want to transform/rotate an UIImage in circular motion without changing the angle of the image. Following diagram explains the requirement.
How this can be done using UIView transform property. Thanks in advance.
Sounds to me like you only need to use the UIView frame property. However, because you are taking a non-linear path between your source and destination you will likely have to dabble with CoreAnimation.
Check out CAKeyFrameAnimation to animate a property through an arbitrary key path.
Good Luck!
My book provides sample code for animating an image along a path:
http://www.apeth.com/iOSBook/ch17.html#FIGnancyBell
CGMutablePathRef path = CGPathCreateMutable();
// ... define the path using CGPath... functions ...
CAKeyframeAnimation* anim1 = [CAKeyframeAnimation animationWithKeyPath:#"position"];
anim1.path = path;
Help needed now.
I can draw lines with MKPolyline and MKPolylineView, but how to draw an arc or curve lines between two coordinates on the MKMapView?
Great thanks.
Before answering the question it is important to mention that MKOverlayView is deprecated and from iOS7 and later we should use MKOverlayRenderer:
In iOS 7 and later, use the MKOverlayRenderer class to display overlays instead.
We can now break it down on how to implement the arc/curve line:
First, we need to define and add our poly line. Let's create one with 2 coordinates:
let polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
mapView.addOverlay(polyline)
After adding the polyline, MKMapView will want us to provide a proper MKOverlayRenderer in corresponding to the MKPolyline we've created at section 1. The method we need is:
mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
Which basically:
Asks the delegate for a renderer object to use when drawing the specified overlay.
So let's provide that object. We will want to subclass MKOverlayPathRenderer which obviously inherits from MKOverlayRenderer and as documentation states:
Use this renderer when your overlay's shape is defined by a CGPath object. By default, this renderer fills the overlay's shape and represents the strokes of the path using its current attributes.
So if we will use our newly subclass object as is, we will get an out-of-the-box solution which is a solid line from the 1st coordinate to the 2nd one we've defined in section 1, but since we want a curved line we will have to override a method for that:
You can use this class as-is or subclass to define additional drawing behaviors. If you subclass, override the createPath() method and use that method to build the appropriate path object. To change the path, invalidate it and recreate the path using whatever new data your subclass has obtained.
It means that inside our CustomObject: MKOverlayPathRenderer we will override createPath:
override func createPath() {
let polyline = overlay as! MKPolyline
// Getting the coordinates from the polyline
let points = polyline.points()
// Taking the center of the polyline (between the 2 coordiantes) and converting to CGPoint
let centerMapPoint = MKMapPoint(polyline.coordinate)
// Converting coordinates to CGPoint corresponding to the specified point on the map
let startPoint = point(for: points[0])
let endPoint = point(for: points[1])
let centerPoint = point(for: centerMapPoint)
// I would like to thank a co-worker of mine for the controlPoint formula :)
let controlPoint = CGPoint(x: centerPoint.x + (startPoint.y - endPoint.y) / 3,
y: centerPoint.y + (endPoint.x - startPoint.x) / 3)
// Defining our new curved path using Bezier path
let myPath = UIBezierPath()
myPath.move(to: startPoint)
myPath.addQuadCurve(to: endPoint,
controlPoint: controlPoint)
// Mutates the solid line with our curved one
path = myPath.cgPath
}
We are basically done. You might want to consider adding width/stroke/color etc so you could see the curved line.
(Optional) If you are interested in a gradient curved line, there is a really great solution here but there are 2 changes you will have to make in the code in order for this to work:
4.1. After overriding override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext), just before adding the gradient, you will have to add the same code from section 3 but instead of changing the inner path you will have to add it to the provided context:
context.move(to: startPoint)
context.addQuadCurve(to: endPoint,
control: controlPoint)
This basically gives us a curved line we need regarding the gradient coloring.
4.2. Instead of using path.boundingBoxOfPath we will need to use path.boundingBox because:
... including control points for Bézier and quadratic curves.
Unlike boundingBoxOfPath:
... not including control points for Bézier and quadratic curves.
Hope that helps :)
Reading the documentation, it seems that you can create an instance of MKOverlayPathView and assign an arbitrary CGPathRef object to its path property. This path can contain both straight lines and arcs.
I don't know (and the documentation doesn't mention) in what format the coordinates of the path should be (MKMapPoint or CLLocationCoordinate2D come to mind) but you can probably find that out by experimenting a bit.
You answered yourself in your comment already, but I'd like to point everyone at the excellent AIMapViewWrapper project on GitHub which includes sample code for plotting an arc path over a set of coordinates. In that project it's being used to draw the path a plane takes including a shadow path underneath it (and other stuff such as animating a plane along that path). Should come in handy for anyone taking a stab at this.
If you want to:
just draw arc path on map
just draw an image on map
combine arc path and image as overlay on map
So the solutions are:
There are MKPolyline and MKPolylineView to draw lines, MKPolygon and MKPolygonView to draw polygons, MKCircle and MKCircleView to draw circles. No one fit? Where is an arc? Oh, yes. Now the really solution: You create a custom class derived from MKOverlayPathView and override the -createPath method, in the -createPath you create an arc use CGContextAddArcToPoint or others functions you like, and associate the path you just create to the path property of MKOverlayPathView and the custom works are done. Then you add MKPolyline in the map(Yes! just MKPolyline!), Then in the -mapView:viewForOverlay: method you create the custom class take that polyline. Then, just run it, everything is runs as you wish. Magic? you can draw a MKPolyline as an arc!
Just an image? Use MKAnnotationView. That's done! you can do it! I believe that!
In my problem, I want to draw an arc with an image in the overlay. So I create a custom class conforms to the MKOverlay protocol, and a custom class derived from MKOverlayView to draw that overlay. Everything works fine but I can't draw any path on the map! I've set the lineWidth to 1,2,3,4... but it wasn't work! Ah..the solution is set the line width with MKRoadWidthAtZoomScale(zoomScale) function and you can see the path! That's done......
And some tips here:
Use Core Graphic to draw paths, not MapKit coordinates nor UIView coordinates!
In the CG functions just keep in mind the line width should be converted by this function:MKRoadWidthAtZoomScale(zoomScale)
Hope to help
I'm drawing an oblong 'egg' shape (on iOS), and want to use it as a boundary for particles. My thought is to use the curve paths to make a UIView and then use hitTest:withEvent: or pointInside:withEvent: to enforce boundary collision.
The problem is, of course, that UIView is always rectangular. How would you go about checking to see if a point is inside an irregular shape like this?
- (void)drawRect:(CGRect)rect {
int w = rect.size.width;
int h = rect.size.height;
CGContextBeginPath(context);
CGContextMoveToPoint(context, w/2, h/5);
CGContextAddCurveToPoint(context, w*0.1, h/4.3, w*0.1, h*0.82, w/2, h*0.8);
CGContextAddCurveToPoint(context, w*0.9, h*0.82, w*0.9, h/4.3, w/2, h*0.2);
I'm using openFrameworks, for what that's worth. This code is just Obj-C but I'm open to any C++/obj-C++ solutions out there.
If you make a CGPathRef you can use CGPathContainsPoint. You can use that same CGPathRef to render into the context. You could also call CGContextPathContainsPoint on the context containing the path, but depending on when you need to test you might not have a context. And another alternative is the containsPoint selector on UIBezierPath.
If you want to code this from scratch, http://www.softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm goes through a couple of different algorithms that will work for an arbitrary polygon.