UIView animations on a path not linear [duplicate] - ios

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?

Related

How can I animate the drawing of a complex line shape in objective-c

So say I wanted to animate drawing this shape:
Animate it something like the disney logo which seems to "write" the letters.
Ideally, I'd also like to have control over how fast it was drawn (maybe just easing), how would I go about getting that done? Specifically imagine if that link had a line of varying widths.
Not too familiar with drawing things in objective-c so perhaps someone can help point me in the right direction in terms of strategy.
Create a CAShapeLayer and set that shape as its path. Use a CABasicAnimation or a CAKeyframeAnimation to animate its strokeEnd property.

iOS Strech Animation - Where should I start looking?

How can I achieve a stretch animation look?
Where do I need to start looking? CABasicAnimation does not seem to do the trick. Something like this: is the desired effect:
http://inspirationmobile.tumblr.com/post/112168531484/sidebar-animation-by-jacub-antalik-ramotion-com
There is a lot more than a simple animation in this view.
It is used UIKitDynamics, but the smart thing is that the final effect is composed by the single effects of small invisible UIViews inside that view.
The border is a CAShapeLayer made by a combination of bezier path that interpolates those single views.
Each drawing cicle, the CAShapeLayer path is refreshed based on the position of those views.
You can find more about this effect here.
For such a complicated shape animation look into CAShapeLayer and UIBezierPath. Look at this SO thread that might help you to get started at least.

iOS development: interactive graph [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
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.

How to animate layer distance from viewer - user - camera change with perspective?

I'm trying to make an animation very similar to the one at the start of Air bnb iOS app.
Here's a video of the animation : video
The idea is to simulate a layer flying from being very close to the user to end sticking on a far away surface.
I've read some articles talking about manipulating the layer.transform.m34 and the one that helped me more is this one.
By applying perspective and a translation on the z-axis, I managed to get the layer look bigger.
Here's the code I used :
CALayer *aLayer = [CALayer layer];
aLayer.frame = ...
aLayer.backgroundColor = ...
CATransform3D perspectiveTransform = CATransform3DIdentity;
perspectiveTransform.m34 = 1.0f/-250.0f;
perspectiveTransform.m44 = 0.0f;
perspectiveTransform = CATransform3DTranslate(perspectiveTransform, 0.0f, 0.0f. -100.0f);
aLayer.transform = perspectiveTransform;
The problem is I can't get it to animate back to CATransform3DIdentity .
I'm not used to CoreAnimation so I may be trying a bad approach.
It would help a lot if someone could point me to what I'm doing wrong or to a better solution.
Thanks in advance!
You need to first create your layer and add it to the layer tree. Once the layer is part of the layer tree then implicit animations should work.
I think you may need to do this:
Create layer
Add layer to parent layer
run remaining animation code with performSelector:withObject:afterDelay: so system gets a chance to add the layer to the layer before your code to do implicit animations is run.

Random Animation

I'd like to build a random animation of a imageview on iPad. What i manage to build so far is this:
The arrow represents my imageview. it moves to the bounds of its parent view an changes it direction. Actually this is not what i want. Id like to move the view randomly in it's parent view, in curves. And also adjust its heading, like in this figure:
I've no clue how to solve this and in particular how to generate a nice smooth path. Maybe someone has a hint for me.
Brad Larson answers a question that will give you some insight here: How do I translate parabolically?
Choosing random points is pretty easy and when you add them using a keyframe animation and a path, you get the smoothing you're looking for. You would just need to add the points to the path reference.
You add the path to the keyframe animation according to the link above and then add the animation to the UIImagView's layer with:
[[imageView layer] addAnimation:pathAnimation forKey:#"pathAnimation"];
Best regards.
Try out with Bezier curves with following
Drawing bezier curves with my finger in iOS?
and
CGPathReference
You would actually get quite far by just configuring the calculation mode of the key frame animation to be cubic
positionAniamation.calculationMode = kCAAnimationCubic; // or kCAAnimationCubicPaced
That will cause it to construct cubic splines between the points specified in the values array.

Resources