iOS development: interactive graph [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
Please take a look at the image above. The red circles represent user's touching motion. So if the user touched where the first circle is (to the left), then there should be a graph that's skewed to the left. If user touched in the middle, a normally distributed graph. If the user touches the right part, the graph should be skewed right.
I am new to iOS development and I am wondering how I can achieve such feature. Any recommendation to suitable libraries, frameworks, documents will be greatly appreciated.

OK, let's go through the components of that one at a time.
To know where the user touches, you could use something like a UIPanGestureRecognizer or UITapGestureRecognizer.
To draw a curve, a common technique is to add a CAShapeLayer:
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = [UIColor blueColor].CGColor;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.lineWidth = 4.0;
[self.view.layer addSublayer:shapeLayer];
You can then set the path for that CAShapeLayer, and every time you change the path, the curve on the view will change. If you change that path frequently (e.g. you use a UIPanGestureRecognizer that is called every time the user's finger moves), you are effectively animating the change of the curve.
So, how do you create the path? Well, you just create a UIBezierPath
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:...];
// build the path as a series of curves
//
// [path addCurveToPoint:...
// controlPoint1:...
// controlPoint2:...];
// or a series of lines that are so numerous that it looks like a nice curve
//
// for (NSInteger x = 0; x <= view.bounds.size.width; x++) {
// [path addLineToPoint:...];
// }
shapeLayer.path = path.CGPath;
So, the only question is whether the curve needs to be a normal distribution, or whether you just want something that draws a smooth curve with a local maximum where the user touched.
If you want something that exactly and mathematically represents some particular curve (e.g. if in the center, it's a true normal distribution), then you need to come up with the equation to represent your curve (as a function of where the user tapped). Then you draw your curve by plotting a whole bunch of points using addLineToPoint with a UIBezierPath.
If you only need something that looks close, then you can approximate it with two bezier paths (created with addCurveToPoint:controlPoint1:controlPoint2:).
We're not going to write this for you, but these are the basic building blocks of the solution. I'd suggest you take these one at a time, do a little research on each, try it out, and then refine. Start small and build from there.

Related

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.

Is it possible to assign color to targeted coordinates x to y without having to upload a UIimage [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to know whether it is possible to assign colors to different coordinates in the IOS and how it can be accomplished. In other words for example making a black square by assigning colors to x,y coordinates.
As per you question, I have understood that you are looking for API to fill a pixel with a color based on the coordinates. To achieve this you can use UIBezierPath class. I wrote a sample example to achieve this.
I have created a subclass of UIView called CustomView and overriden drawRect method like below :
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 100, 100)];
[[UIColor blackColor] set];
[path fill];
}
UIBezierPath class can be used to lay your custom drawing on your view. A path basically represents an area where you can fill different colors in it. The above example will draw a rectangle at x = 50 y = 50 coordinates with width and height of 100 each.
If you want to just fill color to a point, then create bezier path instance like below :
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 1, 1)];
Replace your's View Controller's class name from UIView to CustomView. When you run your app you should see following output :
Your question is too vague and doesn't really make sense. Yes, you could develop an app that stored a grid of pixel colors and then drew them to the screen. However a UIImage or a CGImage (the underlying data structure behind a UIImage) is a good native way to represent pixel data on iOS.
What you typically do is create an off-screen graphics context and draw into that context, then extract an image from the context.

Efficient animation of time-variant drawing using CAKeyFrameAnimation

My current goal is to animate the drawing of a picture in my iOS app so that a user could see something like http://www.youtube.com/watch?v=xlDmk5WdaHk happening on their screen.
The acceleration of my drawings vary with time so I've built my animation using CAKeyFrameAnimation and keyTimes. At the moment, I only have one Bezier path which specifies all the points of my drawing, I add the path to my CAShapeLayer object, and then pass the path off to my animation object when I start my animation.
My problem: the completed drawing immediately appears on the screen, and my pen cursor then moves along the picture according to my keyTimes values, instead of having the stroke color filling in tandem with my pen cursor movement on the screen to produce the same effect as the video.
I noticed this SO question, where the recommended solution was to produce a separate path for every point in the animation: How to Animate CoreGraphics Drawing of Shape Using CAKeyframeAnimation
Given that I have 100-300 individual points, is there a more efficient method of producing a time-variant drawing, than creating an array of a couple hundred paths (with a couple hundred values in each path) with the paths in this form:
path0 = point0...point0...point0...point0...
path1 = point0...point1...point1...point1...
path2 = point0...point1...point2...point2...
path3 = point0...point1...point2...point3...
Then taking that array of paths, passing it into myAnimation.values and animating values with my keyTimes? Or is that really the best way to go about this?
Thanks in advance
There certainly is a more efficient way if your points are on a bezier path. CAShapeLayer has two little properties called strokeStart and strokeEnd. If you initially set strokeEnd to zero then your path will not show. At 1.0 it will show completely. Increasing the values from zero to one will trace your curve accordingly. This is an animatable property so synchronize its value with the key times of your other animation and your drawing will appear as the "pen" is moving.

iOS Calculation control points for bezier path curve animation given two points

I'm trying to animate the movement of a view using CGPathAddCurveToPoint and generating a bezier path but I'm having trouble figuring out the control points between the origin and the destionation. Any of you knows how can I can calculate dynamically the control points given the origin and destionation of the UIView?
For example (iPad), I'm trying to animate a bird (UIView) flying in from point (455,482) to (31,100). I would like to be a curve movement of bird flying instead of lineal movement. I'll really appreciate your help.
thank you for answers but still wonder how can I calculate the control points.
Can you clarify a little about the effect you're looking for? The choice of control points is not only a function of the starting and end coordinates, but also is function of the effect you're trying to achieve. In the past, animating the motion of an imageview where I wanted the illusion of "picking up and dropping" the view, I've done slight perpendicular offset on the control points both in the same direction, which yields a slightly more dynamic motion than a purely linear motion, but it depends entirely upon what you're trying to do. I've also done this with animating the enlarging and then returning to normal of the transform scale at the same time, to further enrich the illusion of picking up and dropping the animated view.
In your revised answer, you say that you want to show the flight of a bird. Now, that's a fascinating problem. Is the view of the bird from the side, e.g. taking off from one branch to another, in which case, your control points probably would both point straight up a little to show the bird taking off and landing. Or if you're trying to simulate the flapping motion, you might also have interim upward control points, one for each flap. Or is it looking at the bird from below or above, in which case a slight "s" shaped flight pattern might be enough, so you would only need have one control pointing 45 degrees from the take off location pointing along the flight path, and the other control point at the end, pointing 45 degrees back towards the start point, but pointing in the other direction. It all depends upon the effect you're looking for given the app's theoretical the relationship between the bird and the viewer in 3d space.
You might need to show a screen snapshot of the scene and the image for the bird (is it from the side, or from above or below). You might also want to draw an example of the flight pattern you're envisaging, and perhaps we can make other, more concrete, suggestions.
Other resources:
By the way, there are some interesting demonstrations of animation that might be helpful. For example this race car animation by Mike Nachbaur was helpful when I did my first animation, showing me how to draw bezier paths, how to rotate the image along the path, too, etc. I know it's not the flight of a bird, but it's still illustrative. I'm sure there are other great examples online, too. Anyway, these sorts of tutorials might help you bridge the gap between your vision of your app and what the bezier paths might look like.
//Draw points
UIBezierPath *aPath = [UIBezierPath bezierPath];
[aPath setLineWidth: LINE_SIZE];
for (int i=0; i<[results count]; i++) {
CGPoint endPt = [[results objectAtIndex: i] CGPointValue];
if (i == 0) {
[aPath moveToPoint:endPt];
} else {
//Previous (start) point
CGPoint startPt =[[results objectAtIndex: i-1] CGPointValue];
//Default control points
CGPoint cPt1, cPt2;
if(ABS(startPt.x - endPt.x) > ABS(startPt.y - endPt.y)) {
cPt1 = (CGPoint){(startPt.x + endPt.x) / 2, startPt.y};
cPt2 = (CGPoint){cPt1.x, endPt.y};
} else {
cPt1 = (CGPoint){startPt.x, (startPt.y + endPt.y) / 2};
cPt2 = (CGPoint){endPt.x, cPt1.y};
}
//Add curve to end point
[aPath addCurveToPoint: endPt controlPoint1: cPt1 controlPoint2: cPt2];
}
}
[aPath stroke];

UIView animations on a path not linear [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I animate the movement of a view or image along a curved path?
I have an iOS application that I want to animate a falling leaf (or several). I have my leaf image in an ImageView and I've figured out a simple animation from the documentation:
[UIView animateWithDuration:4.0f
delay:0
options:UIViewAnimationTransitionFlipFromLeft
animations:^(void)
{
leaf1ImageView.frame = CGRectMake(320, 480, leaf1ImageView.frame.size.width,leaf1ImageView.frame.size.height);
}
completion:NULL];
This will make the leaf go from its starting position to the bottom right corner in a straight line. How would I animate this to follow a path or curve like a parabola or sinusoid and maybe even rotate the image or view? Would this be done in the animations block? Thanks in advance!
You can do this with a CAKeyframeAnimation and a CGPath. The code looks something like this:
CAKeyframeAnimation *leafAnimation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
leafAnimation.duration = 10.0;
leafAnimation.path = /* your CGPathRef */;
[leaf1ImageView.layer addAnimation:leafAnimation forKey:#"leafAnimation"];
The thing you are looking for is animating along a Bezier CGPath. For more details see this question:
How can I animate the movement of a view or image along a curved path?
Creating bezier paths in code is rather hard though, for easier ways to create a bezier curve to animate along look through the answers to the question here:
Drawing bezier curves with my finger in iOS?

Resources