Motion blur during CABasicAnimation - iphone

I have this simple animation that moves an image from one point to another:
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:#"position"];
anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(px2.x, px2.y)];
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(P3x, P3y)];
anim.duration = 1.5f;
anim.repeatCount =1;
anim.removedOnCompletion = YES;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[LA addAnimation:anim forKey:#"positionTest"];
The problem is that the image becomes blurry while in motion, the motion is not very fast.
Is there a way to reduce this motion blur?

If you take a screenshot during the animation is it definitely blurred? Also is the blurring a pixellated blur or is it more like a soft gaussian blur?
Try setting view.layer.shouldRasterize = yes and layer.rasterizationScale = UIScreen.mainScreen.scale;

Related

Flipping two UIView using CATransform3D

I have created Visiting Card which has Front view & back View. When i tap on the front view it should flip the UIView & show the back side of the View.
Here is Code which I am trying:
CATransform3D transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
transform.m34 = 1.0/700.0;
CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:#"transform"];
rotation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
rotation.toValue = [NSValue valueWithCATransform3D:transform];
rotation.duration = DURATION;
CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:#"position"];
translation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.imageview.center.x,[[self.imageview superview] center].y-45)];
translation.toValue = [NSValue valueWithCGPoint:CGPointMake(_frame.origin.x+_frame.size.width/2,
_frame.origin.y+_frame.size.height/2)];
translation.duration = DURATION;
CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:#"position"];
translation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.imageview.center.x,[[self.imageview superview] center].y-45)];
translation.toValue = [NSValue valueWithCGPoint:CGPointMake(_frame.origin.x+_frame.size.width/2,
_frame.origin.y+_frame.size.height/2)];
translation.duration = DURATION;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = #[ translation, rotation ];
group.duration = DURATION;
group.delegate = self;
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[layer addAnimation:group forKey:nil];
Above code works perfectly for single view. How to combine the 2nd view to get flipping from front & back effect.
Using Core Animation
You can add both front and back to a container view. The back view will have a 180 degree rotation around Y and the front will be just normal. Both layers will be single sided (by setting layer.doubleSided = NO;.
Then when you apply the rotation you would animate the rotation of the container view so that both front and back animate at the same time.
UIView transitions
Or you could just use the built in flip animation
transitionFromView:toView:duration:options:completion:
and pass either UIViewAnimationOptionTransitionFlipFromLeft or UIViewAnimationOptionTransitionFlipFromRight for the option.

Smoother Slow Rotating UIImage

I'm trying to make an iPhone/iPad app, and I am currently trying to have an image spin slowly 360 degrees. But when I increase the animation duration, the picture rotates 1/4 of the way and then resets back to the beginning of the animation.
Can anyone tell me what is wrong with this?
What I have so far:
CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
[fullRotation setFromValue:[NSNumber numberWithFloat:0]];
[fullRotation setToValue:[NSNumber numberWithFloat:((2*M_PI))]];
fullRotation.speed = .5f;
fullRotation.duration = 5.5;
fullRotation.repeatCount = 1;
fullRotation.repeatCount = HUGE_VALF;
[[cosmic layer] addAnimation:fullRotation forKey:#"transform.rotation"];
Just delete this one line from your code:
fullRotation.speed = .5f;
and then adjust duration you want.

Moving an image using Core Animation

Any idea why the small image that I move across the screen gets blurry?
While being in motion, the small details in the image become unclear and I think that it is a problem.
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:#"position"];
anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(px2.x, px2.y)];
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(P3x, P3y)];
anim.duration = 1.5f;
anim.repeatCount =1;
anim.removedOnCompletion = YES;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[LA addAnimation:anim forKey:#"position"];
Eventually this was identified as LCD panel lag.

(iphone) how to animate several images with speed variations between images?

this must have been asked and answered but couldn't find an answer.
What I'd like to do is basically animating images like
image1 0.1sec
image2 0.3sec
image3 0.2sec
image4 0.5sec
I have looked at CAKeyFrameAnimation but can't be sure if that's what I needed.
(have almost none image/animation programming background)
Thank you
CAKeyFrameAnimation is for one object to animate differently in different phases of one animation. For example, your object first moves to a point, then continues the same animation to another point, then one another.
You can use CABasicAnimation with different duration values for each image you want to animate.
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:#"position.x"];
anim.delegate = self;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
anim.toValue = [NSNumber numberWithFloat:50.0];
anim.duration = 0.1;
[image1 addAnimation:anim forKey:#"position.x"];
anim.duration = 0.3;
[image2 addAnimation:anim forKey:#"position.x"];
anim.duration = 0.2;
[image3 addAnimation:anim forKey:#"position.x"];
anim.duration = 0.5;
[image4 addAnimation:anim forKey:#"position.x"];

CABasicAnimation not changing its position property after animation completes

i am applying CABasicAnimation for position property of a layer it is animating well but after animation complete its coming back to original position how to avoid that and to make image to stay in animated position only?
There are a few different ways to solve this. My favorite is to set the object at the end of the animation, and use fromValue instead of toValue to create the animation. You can also set both fromValue and toValue to create this effect. ie
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"position"];
animation.duration = 1.0;
animation.fromValue = [NSValue valueWithCGPoint:startPoint];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
viewToAnimate.center = endPoint;
[viewToAnimate.layer addAnimation:animation forKey:#"slide"];
The other way to do this is to set yourself as the delegate and implement:
-(void)animationDidStop:(id)sender finished:(BOOL)finished {
viewToAnimate.center = endPoint;
}
You need to set the final position. If you only use the animation fromValue and toValue it will snap back to the original starting point. The animation uses a copy of the view/layer as it "plays" and then the animation will show the original view/layer when it finishes.
If you don't explicitly set it, it will appear to snap back, even though the view/layer never really moved.
startPoint and endPoint are CGPoints and myView is the UIView that is being animated.
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"position"];
animation.duration = .3;
animation.fromValue = [NSValue valueWithCGPoint:startPoint];
animation.toValue = [NSValue valueWithCGPoint:endPoint];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[myView.layer addAnimation:animation forKey:#"position"];
myView.layer.position = endPoint; // Must set end position, otherwise it jumps back
To set the object at the end of the animation just write the code like this
#import <QuartzCore/QuartzCore.h>
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"position"];
animation.duration = 1.0;
animation.fromValue = [NSValue valueWithCGPoint:startPoint];
animation.timingFunction = [CAMediaTimingFunction functionWithName:easing];
animation.fillMode = kCAFillModeForwards; // This line will stop the animation at the end position of animation
animation.autoreverses = NO;
viewToAnimate.center = endPoint;
[viewToAnimate.layer addAnimation:animation forKey:#"slide"];
You can just set removedOnCompletion to NO if you want to preserve the final value. And don't forget to set the fillMode.
Swift 4+
animation.fillMode = .forwards;
animation.isRemovedOnCompletion = false
Objective-C
animation.fillMode = kCAFillModeForwards; //The receiver remains visible in its final state when the animation is completed.
animation.removedOnCompletion = NO;
If you look at the docs for addAnimation:forKey:, it says 'Typically this is implicitly invoked'. In other words, you're not supposed to call it directly. You're actually meant to do the following instead, which is even easier than setting from and to values, you simply set the values directly on the layer:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"opacity"];
animation.duration = .3;
myLayer.actions = #{#"opacity": animation};
myLayer.opacity = 0;
This will fade a layer to zero opacity.