CAKeyFrameAnimation not repeat - iphone

Hi I have a CAKeyFrameAnimation scale, that makes an object fade down from full size (1) to near nothing (0.01)
Then this is called:
- (void)animationDidStop:(CAKeyframeAnimation *)anim finished:(BOOL)flag
{
[self setHidden:YES];
}
It seems to hide the object, but not before making it re-appear again as full sized for a split second, which ruins the fade to small/nothing effect :P
How do I stop the animation from going back to frame 1 for the "animationDidStop" is called? Thanks!

I believe you need to set the fillMode property of your animations to kCAFillModeForwards. That should freeze the animations at their end time.
Another suggestion (and honestly, this is what I'd usually do) is just set the properties of the layer itself to their final position after you've set up the animation. That way when the animation is removed, the layer will still have the final properties as part of its model.

You can set .scale object property instead of CAKeyframeAnimation.
You can disable animations and set target scale after creating CAKeyframeAnimation.
You can set scale (0.1) in animationDidStop method (do not sure if it will work)
The point is to set target properties of the object. It jumps to them after finishing the animation.

Related

Strange behaviour in UIView Animation

I am facing a strange behavior in UIView animation. I am developing an iPad application which is using some UIView animations.
The duration of all animation is set to 0.5. Initially on launching the application all animations are working fine. But after some continuous use no animation is happening, all UIView changes are happening quickly, just like the duration is not set in animation.
I am not sure why this is happening. Has anyone else faced this kind of issue?
Following is one of the animation I am using. Like this I am using a lot of animations but after some time none of the animation is happening but all the codes inside the animation block is working fine
[UIView animateWithDuration:0.5 animations:^{
[tempLabel setFont:titleFont.font];
[tempLabel setTransform: CGAffineTransformMakeRotation((-90 * M_PI / 180))];
tempLabel.frame = CGRectMake(2,0,23,30);
}];
From the UIView class reference, the frame property:
Changes to this property can be animated. However, if the transform property contains a non-identity transform, the value of the frame property is undefined and should not be modified. In that case, you can reposition the view using the center property and adjust the size using the bounds property instead.
Don't animate the frame when you've also set a transform. Use bounds and center instead.
The problem you have seems that you are transforming the control every time, when you apply a transformation for the first time, there is no transformation on control, but next you have a transform, in this case the next transformation may not work as you want.. You have few options to solve this as follows, I haven't tried the code it might work
tempLabel.transform = CGAffineTransformIdentity;
Put the above code before applying any transformation this will reset any transformation applied and your new transformation will be freshly applied or, you can do
tempLabel.transform = CGAffineTransformConcat(tempLabel.transform, YOUR TRANSFORM);
The above will append to your last transformation..
Hope it helps.
Thanks.

How can I move a UIView, but leave its content positions unchanged relative to the superview?

A UIView has 2 images as sub-views, and clip to bounds set to yes.
I want to move the UIView in an animation block, but have the sub-views not move with the parent. If the view was to move 50 points to the right, more of the second image would be seen, and less of the first.
I can move the view manually, and update the subview positions opposite the new view position, but I can't figure out how to achieve the same thing in a block.
I think the limitation you're running into has to do with the fact that UIView Animation blocks use Core Animation under the hood, and Core Animation animates a 'presentation' layer which is a copy of the original view/layer that the animation was scheduled on. So during your block's animation, the presentation layer's position is changing, but the position of the model view remains unchanged.
Since you mentioned that you can update the view position manually (and subview positions in the opposite direction) to achieve the desired effect, consider creating an NSTimer at animation schedule time that runs 60 times per second (to try and catch every animation frame) that simply queries the presentation layer for its current position (as set by the animation itself + any special timing functions specified as argument to the animation), and perform your work to update the underlying model to match that.
You should be able to query the animated layer's position with:
yourUIView.layer.presentationLayer.frame.origin
Then add a completion block to your animation that invalidates the timer.
Before you can access the view's layer property, you must add to your project the
QuartzCore.framework
Cheers,

iPhone animation based on input values (touches) not time

For an animation effect perfectly suited to an animation group approach as shown in Brad Larson's answer here, I need the animation to proceed according to inputs. Specifically touch and position of detected touches. It is easy to handle touchesMoved: and to set the position of the elements for every touch but it just isn't smooth like the core animation approach.
Imagine a marble in a grooved track. I want to push the marble along to any position at any speed in one direction or the other. The animation has to do something like that, moving a visual element along a path in response to touches. CAKeyframeAnimation has the path bit exactly but seems to always want to base the transition from frame to frame on time elapsed, not on any other factor, and in one direction.
31 January update - Thanks all for the responses so far however none is really solving the problem. I have a circular menu that is dragged to select an option. All of it needs to move together and I have worked around it by using an view that has a rotational transform applied and the inverse rotational transform applied to its subviews so the icons all rotate with appropriate ferris wheel orientation. It really looks better when the icons are animated along a slightly ovoid path though... the marble description is an attempt to make clear what I'm trying to do. Better perhaps to imagine magnets oriented to repel all travelling in a groove - move one and its neighbours move too but not necessarily in the direction that the dragged magnet moves as the path curves.
Right now the problem is one of following a simple path created with one circle but I'd really like to know how to animate objects along an arbitrary path, position controlled purely by touch with no calculations involving velocity or direction.
You may be able to use the hierarchical nature of timelines in layer trees to achieve what you’re looking for. Objects implementing CAMediaTiming, which include both CAAnimation and CALayer, inherit a timespace from their parent, which they can modify (via scaling, shifting and repeating) and propagate to their children. By setting the speed property of a layer to 0.0 and adjusting the timeOffset property manually, you can decouple that layer (and all of its sublayers) from the usual notion of time.
What you’d do in your case is define the animations for all your menu items to animate their position along your desired CGPath from time t0 to t1, with the appropriate timeOffset on each animation to keep your items appropriately spaced. Note that a beginTime of 0.0 on an animation is usually interpreted as starting from the time the animation was added to its layer, so if you want t0 to be 0.0, you'll probably have to set it to a tiny epsilon > 0.0.
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
animation.beginTime = 1e-100;
animation.duration = 1.0;
animation.fillMode = kCAFillModeBoth;
animation.removedOnCompletion = NO;
animation.path = path;
animation.calculationMode = kCAAnimationPaced;
animation.timeOffset = timeOffset;
You’d then set the speed property to 0.0 on a parent layer (containing only these menu items) and update its timeOffset to values between t0 and t1 in response to your touch events.
This approach has two potential caveats. Because you’ve taken over the nature of time on this layer subtree, you probably won’t be able to animate other properties at the same time. Additionally, if you want coasting behavior for a fast flick, you’ll probably need to animate time forward on your own.
If you are targeting iOS 4.0 or greater then you can use the new block based class methods to start animatable changes to view objects. So from your touch event you can initiate an animatable change to properties on the view:
[UIView animateWithDuration:1.0 animations:^{
yourView.alpha = 0.0; // fade out yourView over 1 second
}];
N.B. This could just as easily be a change to another property on the view, like its location. You can animate the following properties on the view this way:
#property frame
#property bounds
#property center
#property transform
#property alpha
#property backgroundColor
#property contentStretch
If you are targetting earlier versions of iOS you will need to use UIView beginAnimations and commitAnimations methods to create an animation block:
[UIView beginAnimations:nil context:context];
[UIView setAnimationDuration:1.0];
yourView.alpha = 0.0;
[UIView commitAnimations];
This stuff works really well and once you start using it, you will have to be careful you don't get addicted. ;)
Update for comment:
You can bind the position of the marble to the location of the touch event. Once you get the touchesEnded event you can then animate the location of the marble using an animation block.
velocity = distance/time. You can try by giving delay according to touches moved and time. You can calculate time between touchesBegan and touchesEnded methods.
I'm not entirely certain I understand exactly what you want to do but... Why not just draw the marble wherever the user's finger is? No animation required.
If you want to keep the marble moving after the user lets go, you need to maintain a concept of momentum and use that to animate the marble slowing down. You could use either CoreAnimation for that or a timer.
I would make a velocity calculation in touches moved, and add it to a local variable that a block can mutate via a timer.
in other words, in touches moved make a velocity calculation bearing in mind the direction of a previous velocity. In touches ended, fire a block that translates the 'marble' decaying the velocity as you disire. If the user moves the marble again, modify the local variable, this will in turn speed up the animation of the marble or slow it down/change direction depending on on the direction of the touch.

How to animate stuff when using -drawRect:?

I didn't use subviews but painted my things with -drawRect: inside an UIView subclass. Now I want to do some animations in there.
I guess that I can't count on core animation now since I have no subviews. So how would I animate then? Would I set up a timer which fires like 30 times per second? How would I know the animation step? Would I make an ivar which counts the frame of the animation so that I can do my stuff in -drawRect as it gets called?
You can do exactly that: have an instance variable with the frame number and increment it in the timer. If you don't care about easing, you can then just multiply the frame number by a speed and then set that to the property which you want to animate.
-(void)timerfunction{
++frame;
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect{
CGContextFillRect(UIGraphicsGetCurrentContext(),CGRectMake((2*frame)+5,5,20,20));
}
This works but sounds like crazy.YOu draw the same content hunderts of times and it could cause performance issues in case of TableView.I would refer to CALayer animations

Immediate manipulation of UIView animatable properties during rotation?

I am trying to accomplish some complex effects during my UIView rotation, and I'm using the first half/second half method of rotation animations.
However, when my second half starts, I'd like to set some properties on my subviews (alpha, frame) etc, from which to begin animating in the second half. But setting any of these properties of course causes them to be animated. I'd like to say, set the alpha to 0.0, and THEN say, "ok, now animate it to 1.0 throughout the rest of the rotation."
Note that I can't set this property before the whole rotation; I want to affect its immediate value partway through.
Can this be done?
Thanks.
Implement this function in your UIControllerView :
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration