What is the difference between CGPath and UIBezierPath()? [closed] - ios

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
At the moment, I'm trying to work out how to animate a custom button which I have an image of and have the coordinates of but I've found that you can create a button/object both by using the CGPath class or the UIBezierPath class. Can somebody please tell me what the difference between the two are?

CGPath is an opaque type from the CoreGraphics library, while UIBezierPath is an Obj-C class in UIKit. UIBezierPath is a wrapper around CGPath (you can convert between them easily), with a more object-oriented interface and a few convenience methods. Using CGPath may be marginally faster as it doesn't have to go through Obj-C (although who knows how it works with Swift) and it has more advanced functions like CGPathApply. Importantly, UIBezierPath conforms to NSCoding, meaning you get serialization/deserialization for free.
tl;dr: use UIBezierPath unless you have some reason not to.

To expand a bit on what jtbandes wrote, there's another distinction: CGPath is just a mathematical description of a shape. It has no drawing-specific properties or capabilities. Instead, drawing properties are part of the CGContext as is the actual drawing operation. You set things like color, line width, etc. on the context before the drawing operation, but those are not part of the path itself.
By contrast, UIBezierPath has drawing properties and drawing operations.

Related

iOS: How to partially change the color of text [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was playing with iOS built-in Compass app and the UI do make me curious.
Here is the interesting part:
The color of text (even the circle) can be partially and dynamically changed.
I have made a lot of search, but the results are all about attributed string. How to implement the effect like this?
Edited:
I have tried to add two UILabels (whiteLabel and blackLabel) as whitelabel at the bottom and blackLabel at the top with the same frame. Then I set the circle as the mask of blackLabel.
The problem is 'whiteLabel' is totally covered by blackLabel, and if the circle do not intersect with 'blackLabel', both labels are not visible.
I imagine that there are two "14" labels in the same place. The bottom one is white and unmasked, and the top one is black and has a layer mask that contains two circles, so it's only visible where the circles are.
Achieving this has most probably nothing to do with NSAttributedStrings, like Woodstock said.
I'd say it's the UILabel's layer that is recolored live, depending on what other layer it intersects with, and the overlaying area of said intersection.
Once you figure those common points, you just apply a mask that inverts colors from there.
Now it's a little bit more complicated than that since there appears to be two circles (hence two layers to find intersections with), but in the end, it's "just a list of coordinates" that the label's coordinates intersects or not.
That could be an interesting exercise ; it would probably take me a decent amount of tries to mimic that behaviour, but I'm pretty confident my reasoning is on point. (get it? :o)

How can I throw in sprites from both sides of the screen in Swift? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to apply a sort of impulse onto a sprite and have it thrown onto the screen from the side. However, when it is after the screen I want the impulse to stop and for the regular physics to be applied to it, so I would be able to drag it around. Its difficult to explain but if anyone can help out it would be very much appreciated. Thanks!
Apple docs show that a we can influence the physicsBody of a sprite with these instance methods:
- applyForce:
- applyTorque:
- applyForce:atPoint:
- applyImpulse:
- applyAngularImpulse:
- applyImpulse:atPoint:
As for "throwing it on the screen from the side", you can have the sprite spawn outside your current skViews bounds, and have the force be applied to it accordingly (if the sprite is located outside the north side of the screen, you'll want to applyImpulse downward).
With respect to "regular physics" upon entrance of the skView, you can have a check in update:(CFTimeInterval)currentTime for those types of sprites entering the skViews frame OR define a protocol within your sprite's class, and have your SKScene subclass conform to that protocol; then have your sprite fire a method when entering a certain frame (skView.frame), which your SKScene will be able to respond to. You can then apply counter forces or impulses to that sprite.

How to use UIBezier Path For Making UIButton [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have to make UIButton in Specify Shape.That Shape have to convert in UIButton How Can i do..plz Help..!!
One suggestion would be to create the shape you want using a CAShapeLayer and add a touch detector to it that triggers an action.
If you're unfamiliar with CALayer and its many applications Ray Wenderlich has an excellent overview here.
First of all UIBezier Path is not For Making UIButton it is for giving a shape for UIButton.
Second, if you want to give shape to your UIButton then Apple provided all detail regarding BezierPaths in their document.
As ican understand you need to shape button like Curve shape then on Apples document provided method here .
#define DEGREES_TO_RADIANS(degrees) ((pi * degrees)/ 180)
- (UIBezierPath *)createArcPath
{
UIBezierPath *aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)
radius:75
startAngle:0
endAngle:DEGREES_TO_RADIANS(135)
clockwise:YES];
return aPath;
}
There are two mainly curve provided by apple and inbuilt method in beizear path.
You need to use Quadratic curve to shape your UIButton.
Please check this link.
addQuadCurveToPoint:controlPoint
and Use inbuilt method and change endpoint and controlpoint According to your shape.
Note:- You need to change the point according to your Requirement.

Motion paths drawn by finger [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am interested in making path with finger (like connecting objects) between two elements. I am not sure how would I start on this.
I know that I could use Bezier path to create lines, but I am not sure how to create that line with finger. Does anyone have some good example?
I tried to google it, but I can't find anything like that made.
Thanks
I answered a question recently about slow/laggy performance on a similar setup. I.E. Drawing UIBezierPaths in CALayer. The answer contains a subclass of UIView which you can drop into a storyboard and will pretty much get you started. The header file is not shown in the answer, but it is literally a subclass of UIView (just add a UIView Subclass to your project). You should be able to copy the rest into your implementation file. Obviously you'll want to take out the performance testing code.
touchesMoved drawing in CAShapeLayer slow/laggy
If you simply want to Add a single line, you just need to get the starting point in touchesBegan, and build the path in touchesMoved. The commitCurrentRendering simply renders the touch points accumulated, then clears the UIBezierPath. This improves the performance as there wass a notable slowdown when UIBezierPath reached around 2000 points (touchesMoved will feed you a succession of points as your finger moves).

is it possible to make custom shaped buttons in iOS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Or some kind of UI objects that can
interact with users
be drawn into any shape, for example, oval.
yes. It is pretty simple.
UIImage *buttonImage = [UIImage imageNamed:#"greenButton.png"];
UIImage *buttonImageHighlight = [UIImage imageNamed:#"greenButtonHighlight.png"];
[myCustomButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[myCustomButton setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
It is possible to do it in a variety of ways. Rydgaze gave you the option for using a UIImage as your custom button. If you are looking for a code only way then I would suggest UIBezierPath. UIBezierPath allow's you to draw any shape that you want as well as customize the look. With UIBezierPath I could draw a star and fill it with anything that I would like.
Here is the link to the Reference: https://developer.apple.com/library/ios/documentation/uikit/reference/UIBezierPath_class/Reference/Reference.html
The idea is that you create a view class. In that view's drawRect method you will design whatever shape you are looking to create and customize it to what you see fit. For you button you would set its self.view to be the view that of your custom UIBezierPath class. That will just draw your custom shape as the button.
The advantage's to writing your button's in code are that it will for one be faster. It also takes up significantly less size because it isn't storing an image file. Also you are able to continue the customization of your shape. That mean's you could change the color, dimensions, etc. very easily.

Resources