I need to rotate an UIImageView continuously. To do this I found this code :
if ([self.image.layer animationForKey:#"SpinAnimation"] == nil) {
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:#"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2 * M_PI];
animation.duration = 90.0f;
animation.repeatCount = INFINITY;
[self.image.layer addAnimation:animation forKey:#"SpinAnimation"];
}
Then, I need to make a transform translation on the image view, animating it. If I do this :
CGAffineTransform transform = self.image.transform;
transform = CGAffineTransformTranslate(transform, 0, 350);
[UIView animateWithDuration:1.0f animations:^{
[self.view layoutIfNeeded];
self.image.transform = transform;
} completion:^(BOOL finished) {
self.isMiddleViewOpened = YES;
}];
When the animation is performed, the image float all around the view before arriving to the end point of the translation.
Thanks ☺️
EDIT
I have issue because in my second transform I edit only the y value, but the image doesn't move only on y axis. If you try this code, you'll see the issue
There is no way to mix both animations, you can use Core animation to do all your animation stuff.
I use this in my splash screen to rotate four image blocks and combine them into one in the end.
The animation would like this:
Code for one image block: Top left block , I call it 00 block
- (void)addSplashScreenAnimationWithCompletion:(void (^)(BOOL finished))completionBlock
{
[self addSplashScreenAnimationWithBeginTime:0 andFillMode:kCAFillModeBoth andRemoveOnCompletion:NO completion:completionBlock];
}
- (void)addSplashScreenAnimationWithBeginTime:(CFTimeInterval)beginTime andFillMode:(NSString *)fillMode andRemoveOnCompletion:(BOOL)removedOnCompletion completion:(void (^)(BOOL finished))completionBlock
{
CAMediaTimingFunction *linearTiming = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
if (completionBlock)
{
CABasicAnimation *representativeAnimation = [CABasicAnimation animationWithKeyPath:#"not.a.real.key"];
representativeAnimation.duration = 8.500; //your duration
representativeAnimation.delegate = self;
[self.layer addAnimation:representativeAnimation forKey:#"SplashScreen"];
}
CAKeyframeAnimation *_00RotationAnimation = [CAKeyframeAnimation animationWithKeyPath:#"transform.rotation.z"];
_00RotationAnimation.duration = 8.500;
_00RotationAnimation.values = #[#(0.000), #(18.829), #(18.829)];
_00RotationAnimation.keyTimes = #[#(0.000), #(0.353), #(1.000)];
_00RotationAnimation.timingFunctions = #[linearTiming, linearTiming];
_00RotationAnimation.beginTime = beginTime;
_00RotationAnimation.fillMode = fillMode;
_00RotationAnimation.removedOnCompletion = removedOnCompletion;
CAKeyframeAnimation *_00OpacityAnimation = [CAKeyframeAnimation animationWithKeyPath:#"opacity"];
_00OpacityAnimation.duration = 8.500;
_00OpacityAnimation.values = #[#(0.000), #(0.497), #(0.553), #(0.759), #(0.921), #(1.000), #(0.642), #(0.341), #(0.000), #(0.000)];
_00OpacityAnimation.keyTimes = #[#(0.000), #(0.059), #(0.118), #(0.176), #(0.235), #(0.353), #(0.471), #(0.647), #(0.765), #(1.000)];
_00OpacityAnimation.timingFunctions = #[linearTiming, linearTiming, linearTiming, linearTiming, linearTiming, linearTiming, linearTiming, linearTiming, linearTiming];
_00OpacityAnimation.beginTime = beginTime;
_00OpacityAnimation.fillMode = fillMode;
_00OpacityAnimation.removedOnCompletion = removedOnCompletion;
CAKeyframeAnimation *_00ScaleXAnimation = [CAKeyframeAnimation animationWithKeyPath:#"transform.scale.x"];
_00ScaleXAnimation.duration = 8.500;
_00ScaleXAnimation.values = #[#(0.600), #(0.600), #(1.187), #(1.187)];
_00ScaleXAnimation.keyTimes = #[#(0.000), #(0.353), #(0.765), #(1.000)];
_00ScaleXAnimation.timingFunctions = #[linearTiming, linearTiming, linearTiming];
_00ScaleXAnimation.beginTime = beginTime;
_00ScaleXAnimation.fillMode = fillMode;
_00ScaleXAnimation.removedOnCompletion = removedOnCompletion;
CAKeyframeAnimation *_00ScaleYAnimation = [CAKeyframeAnimation animationWithKeyPath:#"transform.scale.y"];
_00ScaleYAnimation.duration = 8.500;
_00ScaleYAnimation.values = #[#(0.688), #(0.688), #(1.359), #(1.359)];
_00ScaleYAnimation.keyTimes = #[#(0.000), #(0.353), #(0.765), #(1.000)];
_00ScaleYAnimation.timingFunctions = #[linearTiming, linearTiming, linearTiming];
_00ScaleYAnimation.beginTime = beginTime;
_00ScaleYAnimation.fillMode = fillMode;
_00ScaleYAnimation.removedOnCompletion = removedOnCompletion;
CAKeyframeAnimation *_00TranslationXAnimation = [CAKeyframeAnimation animationWithKeyPath:#"transform.translation.x"];
_00TranslationXAnimation.duration = 8.500;
_00TranslationXAnimation.values = #[#(0.000), #(107.423), #(211.936), #(211.936)];
_00TranslationXAnimation.keyTimes = #[#(0.000), #(0.353), #(0.765), #(1.000)];
_00TranslationXAnimation.timingFunctions = #[linearTiming, linearTiming, linearTiming];
_00TranslationXAnimation.beginTime = beginTime;
_00TranslationXAnimation.fillMode = fillMode;
_00TranslationXAnimation.removedOnCompletion = removedOnCompletion;
CAKeyframeAnimation *_00TranslationYAnimation = [CAKeyframeAnimation animationWithKeyPath:#"transform.translation.y"];
_00TranslationYAnimation.duration = 8.500;
_00TranslationYAnimation.values = #[#(0.000), #(390.498), #(476.237), #(476.237)];
_00TranslationYAnimation.keyTimes = #[#(0.000), #(0.353), #(0.765), #(1.000)];
_00TranslationYAnimation.timingFunctions = #[linearTiming, linearTiming, linearTiming];
_00TranslationYAnimation.beginTime = beginTime;
_00TranslationYAnimation.fillMode = fillMode;
_00TranslationYAnimation.removedOnCompletion = removedOnCompletion;
}
The above method contains series of animations like rotating, opacity changin,,scaling along x and y axis,translating along x and y axis.
You need to subclass the UIImageView or UIView and put the animation in there.
You can call the above method like:
//Here _splashView can be UIView or UIImageView
[_splashView addSplashScreenAnimationWithCompletion:^(BOOL finished) {
//Do your stuff after animation is completed
}];
You can use repeatCount for CAKeyFrameAnimation too, and you should give HUGE_VALF to get the infinity time rotation.
I'm trying to add an Animation to a UIView. The objective of the animation is to "animate" the View appearing on screen (instead of just appearing there).
The animation is all size scaling: start from 5%, go up to 120% and then very quickly back to 100% of the regular scale.
My issue is that the full scale UIView appears very quickly, before the animation starts.
Here's the code:
UIView * myView = [[UIView alloc] initWithFrame:someFrame];
[self.view addSubview:myView];
[self initialAnimationFor:myView];
-(void) initialAnimationFor:(UIView*)pView {
const CFTimeInterval firstDuration = 0.75f;
const CFTimeInterval secondDuration = 0.025f;
const float initialValue = 0.05f;
const float middleValue = 1.20f;
CABasicAnimation * firstAnimation = [CABasicAnimation animationWithKeyPath:#"transform.scale"];
firstAnimation.duration = firstDuration;
firstAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
firstAnimation.fromValue = [NSNumber numberWithFloat:initialValue];
firstAnimation.toValue = [NSNumber numberWithFloat:middleValue];
CABasicAnimation * secondAnimation = [CABasicAnimation animationWithKeyPath:#"transform.scale"];
secondAnimation.duration = secondDuration;
secondAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
secondAnimation.fromValue = [NSNumber numberWithFloat:middleValue];
secondAnimation.toValue = [NSNumber numberWithFloat:1.0f];
CAAnimationGroup *animationGroup = [CAAnimationGroup new];
animationGroup.duration = firstDuration + secondDuration;
animationGroup.animations = #[firstAnimation, secondAnimation];
[pView.layer addAnimation:animationGroup forKey:nil];
}
Any ideas? Thanks!
I would do a different technique and use chained UIView block animations, like this:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(40, 40, 200, 200)];
myView.backgroundColor = [UIColor redColor];
[self initialAnimationFor:myView];
}
- (void)initialAnimationFor:(UIView*)pView {
pView.transform = CGAffineTransformMakeScale(0.05f, 0.05f);
if (pView.superview == nil) {
[self.view addSubview:pView];
}
[UIView
animateWithDuration:0.75f
animations:^{
pView.transform = CGAffineTransformMakeScale(1.20f, 1.20f);
}
completion:^(BOOL finished) {
[UIView
animateWithDuration:0.25f // <-- Your old value of 0.025f makes the animation VERY quick
animations:^{
pView.transform = CGAffineTransformIdentity;
}
];
}
];
}
With this setup, you get the "grow to slightly larger than 100% and then 'settle' to 100%" effect.
Is this a workable solution?
I've got a custom UIViewController transition -- in iOS7, it works perfectly fine.
However, when run on iOS 8, I'm experiencing issues. Specifically, when the presented view controller is dismissed, the original view controller's view disappears entirely - leaving me with a completely blank screen. Here is my code:
#implementation DDCardTransition
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
return 0.5f;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
CGRect endFrame = self.destinationFrame;
if (self.presenting) {
fromViewController.view.userInteractionEnabled = NO;
toViewController.view.layer.cornerRadius = 6.0f;
UIView *tintAdjustmentView = [UIView new];
tintAdjustmentView.frame = fromViewController.view.frame;
tintAdjustmentView.backgroundColor = [UIColor blackColor];
tintAdjustmentView.alpha = 0.f;
[transitionContext.containerView addSubview:fromViewController.view];
[transitionContext.containerView addSubview:tintAdjustmentView];
[transitionContext.containerView addSubview:toViewController.view];
CGRect startFrame = endFrame;
startFrame.origin.y += [UIScreen mainScreen].bounds.size.height;
toViewController.view.frame = startFrame;
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[transitionContext completeTransition:YES];
}];
CABasicAnimation *frameAnimation = [CABasicAnimation animationWithKeyPath:#"position"];
frameAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.1f :0.8f :0.0 :1.0];
frameAnimation.duration = [self transitionDuration:transitionContext];
frameAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(startFrame), CGRectGetMidY(startFrame))];
frameAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(endFrame), CGRectGetMidY(endFrame))];
toViewController.view.layer.position = [frameAnimation.toValue CGPointValue];
[toViewController.view.layer addAnimation:frameAnimation forKey:#"position"];
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
opacityAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
opacityAnimation.duration = 0.3f;
opacityAnimation.fromValue = #(0.f);
opacityAnimation.toValue = #(0.5f);
tintAdjustmentView.layer.opacity = [opacityAnimation.toValue floatValue];
[tintAdjustmentView.layer addAnimation:opacityAnimation forKey:#"opacity"];
[CATransaction commit];
}
else {
toViewController.view.userInteractionEnabled = YES;
UIView *tintAdjustmentView = [UIView new];
tintAdjustmentView.frame = toViewController.view.frame;
tintAdjustmentView.backgroundColor = [UIColor blackColor];
tintAdjustmentView.alpha = 0.5f;
[transitionContext.containerView addSubview:toViewController.view];
[transitionContext.containerView addSubview:tintAdjustmentView];
[transitionContext.containerView addSubview:fromViewController.view];
endFrame.origin.y += [UIScreen mainScreen].bounds.size.height;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
fromViewController.view.frame = endFrame;
tintAdjustmentView.alpha = 0.f;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
}
#end
Any idea why this no longer works in iOS8?
Thanks!
the following code work fine in xcode GM:
func animateTransition(transitionContext: UIViewControllerContextTransitioning!) {
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
let fromViewControllerView = transitionContext.viewForKey(UITransitionContextFromViewKey)
let containerView = transitionContext.containerView()
fromViewControllerView?.frame = transitionContext.finalFrameForViewController(fromViewController!)
fromViewControllerView?.frame.origin.y = containerView.bounds.height
containerView.addSubview(fromViewControllerView!)
UIView.animateWithDuration(transitionDuration(transitionContext),
delay: 0.0,
options: .AllowUserInteraction,
animations: {
fromViewControllerView!.center.y = containerView.bounds.size.height/2
},
completion: { (completed: Bool) -> Void in
transitionContext.completeTransition(completed)
}
)
}
I want to rotate image in circular animation. How can I do it in a simple way?
I know it is not difficult but I'm new to iOS.
Can anybody tell me right code ?
its my solution:
- (void)startAnimation
{
[UIView animateWithDuration:.4 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
[self.loadingRingImageView setTransform:CGAffineTransformRotate(self.loadingRingImageView.transform, M_SQRT2)];
} completion:^(BOOL finished) {
if (finished)
{
[self startAnimation];
}
}];
}
[[self.<imageviewinstance> layer] addAnimation:[self rotationAnimation] forKey:nil];
Add the animation as above and the below is the animation code.
- (CABasicAnimation *)rotationAnimation
{
CABasicAnimation *rotAnim = [CABasicAnimation animationWithKeyPath:#"position"];
[rotAnim setFromValue:[NSValue valueWithCGPoint:CGPointMake(100.0, 400.0)]];
rotAnim.toValue = [NSValue valueWithCGPoint:CGPointMake(300.0, 150.0)];
// rotAnim.duration = 5.0;
rotAnim.autoreverses = YES;
// rotAnim.repeatCount = HUGE_VAL;
rotAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
return rotAnim;
}
if the above is not worked use this
- (CABasicAnimation *)rotationAnimation
{
CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:#"transform.rotation" ];
move.delegate = self;
[move setFromValue:[NSNumber numberWithFloat:0]];
[move setToValue:[NSNumber numberWithFloat:50]];
[move setDuration:10.0f];
move.autoreverses = YES;
}
the answer of question i was wondering is ->
UIImageView *imageViewForAnimation = [[UIImageView alloc] initWithImage:imageToAnimate];
imageViewForAnimation.alpha = 1.0f;
CGRect imageFrame = imageViewForAnimation.frame;
//Your image frame.origin from where the animation need to get start
CGPoint viewOrigin = imageViewForAnimation.frame.origin;
viewOrigin.y = viewOrigin.y + imageFrame.size.height / 2.0f;
viewOrigin.x = viewOrigin.x + imageFrame.size.width / 2.0f;
imageViewForAnimation.frame = imageFrame;
imageViewForAnimation.layer.position = viewOrigin;
[self.view addSubview:imageViewForAnimation];
// Set up fade out effect
CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
[fadeOutAnimation setToValue:[NSNumber numberWithFloat:0.3]];
fadeOutAnimation.fillMode = kCAFillModeForwards;
fadeOutAnimation.removedOnCompletion = NO;
// Set up scaling
CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:#"bounds.size"];
[resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(40.0f, imageFrame.size.height * (40.0f / imageFrame.size.width))]];
resizeAnimation.fillMode = kCAFillModeForwards;
resizeAnimation.removedOnCompletion = NO;
// Set up path movement
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
//Setting Endpoint of the animation
CGPoint endPoint = CGPointMake(480.0f - 30.0f, 40.0f);
//to end animation in last tab use
//CGPoint endPoint = CGPointMake( 320-40.0f, 480.0f);
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, viewOrigin.x, viewOrigin.y);
CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, viewOrigin.y, endPoint.x, viewOrigin.y, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
CAAnimationGroup *group = [CAAnimationGroup animation];
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[group setAnimations:[NSArray arrayWithObjects:fadeOutAnimation, pathAnimation, resizeAnimation, nil]];
group.duration = 0.7f;
group.delegate = self;
[group setValue:imageViewForAnimation forKey:#"imageViewBeingAnimated"];
[imageViewForAnimation.layer addAnimation:group forKey:#"savingAnimation"];
I'm trying to get the hang of the new viewController animations in ios7 but I am encountering a road bump when trying to use core animation.
Im finding that after the transition has completed, the controller of the 'to view' does not seem to be receiving the typical view lifecycle methods, i.e. viewWillAppear and viewDidAppear. I'm calling completeTransition: but that doest seem to be enough -- is this typical behavior? Or am I doing something wrong? here is my code:
-(void)executeDismissalAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
UIView *inView = [transitionContext containerView];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
toViewController.view.hidden = NO;
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[fromViewController.view removeFromSuperview];
[transitionContext completeTransition:YES];
}];
CGFloat duration = 0.4f;
CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:#"transform.scale"];
scaleAnimation.values = [self floatValuesFromValue:1.f toValue:1.5f withEasingFunction:ExponentialEaseInOut];
scaleAnimation.duration = duration;
scaleAnimation.fillMode = kCAFillModeForwards;
scaleAnimation.removedOnCompletion = YES;
[fromViewController.view.layer addAnimation:scaleAnimation forKey:nil];
CAKeyframeAnimation *alphaAnimation = [CAKeyframeAnimation animationWithKeyPath:#"opacity"];
alphaAnimation.values = [self floatValuesFromValue:1.f toValue:0.f withEasingFunction:LinearInterpolation];
alphaAnimation.duration = duration*2;
alphaAnimation.fillMode = kCAFillModeForwards;
alphaAnimation.removedOnCompletion = YES;
[fromViewController.view.layer addAnimation:alphaAnimation forKey:nil];
CAKeyframeAnimation *scaleAnimationB = [CAKeyframeAnimation animationWithKeyPath:#"transform.scale"];
scaleAnimationB.values = [self floatValuesFromValue:0.6f toValue:1.0f withEasingFunction:ExponentialEaseInOut];
scaleAnimationB.duration = duration;
scaleAnimationB.beginTime = CACurrentMediaTime()+0.3;
scaleAnimationB.fillMode = kCAFillModeForwards;
scaleAnimationB.removedOnCompletion = NO;
[toViewController.view.layer addAnimation:scaleAnimationB forKey:nil];
CAKeyframeAnimation *alphaAnimationB = [CAKeyframeAnimation animationWithKeyPath:#"opacity"];
alphaAnimationB.values = [self floatValuesFromValue:0.5f toValue:1.f withEasingFunction:LinearInterpolation];
alphaAnimationB.duration = duration;
alphaAnimationB.beginTime = CACurrentMediaTime()+0.3;
alphaAnimationB.fillMode = kCAFillModeForwards;
alphaAnimationB.removedOnCompletion = NO;
[toViewController.view.layer addAnimation:alphaAnimationB forKey:nil];
[CATransaction commit];
}