Instead of UIView
animation, try using the core animation.
I have created a simple UIView
with grey background, and added the animation below:
- (void)addAlertPopUpAnimationWithBeginTime:(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 = 0.200;
representativeAnimation.delegate = self;
[self.layer addAnimation:representativeAnimation forKey:@"AlertPopUp"];
}
CAKeyframeAnimation *proPicOpacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
proPicOpacityAnimation.duration = 0.200;
proPicOpacityAnimation.values = @[@(0.000), @(0.525), @(1.000)];
proPicOpacityAnimation.keyTimes = @[@(0.000), @(0.500), @(1.000)];
proPicOpacityAnimation.timingFunctions = @[linearTiming, linearTiming];
proPicOpacityAnimation.beginTime = beginTime;
proPicOpacityAnimation.fillMode = fillMode;
proPicOpacityAnimation.removedOnCompletion = removedOnCompletion;
[[self layer] addAnimation:proPicOpacityAnimation forKey:@"alertPopUp_Opacity"];
CAKeyframeAnimation *proPicScaleXAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
proPicScaleXAnimation.duration = 0.200;
proPicScaleXAnimation.values = @[@(0.304), @(0.345), @(0.300)];
proPicScaleXAnimation.keyTimes = @[@(0.000), @(0.500), @(1.000)];
proPicScaleXAnimation.timingFunctions = @[linearTiming, linearTiming];
proPicScaleXAnimation.beginTime = beginTime;
proPicScaleXAnimation.fillMode = fillMode;
proPicScaleXAnimation.removedOnCompletion = removedOnCompletion;
[[self layer] addAnimation:proPicScaleXAnimation forKey:@"alertPopUp_ScaleX"];
CAKeyframeAnimation *proPicScaleYAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
proPicScaleYAnimation.duration = 0.200;
proPicScaleYAnimation.values = @[@(0.381), @(0.435), @(0.389)];
proPicScaleYAnimation.keyTimes = @[@(0.000), @(0.500), @(1.000)];
proPicScaleYAnimation.timingFunctions = @[linearTiming, linearTiming];
proPicScaleYAnimation.beginTime = beginTime;
proPicScaleYAnimation.fillMode = fillMode;
proPicScaleYAnimation.removedOnCompletion = removedOnCompletion;
[[self layer] addAnimation:proPicScaleYAnimation forKey:@"alertPopUp_ScaleY"];
CAKeyframeAnimation *proPicTranslationXAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
proPicTranslationXAnimation.duration = 0.200;
proPicTranslationXAnimation.values = @[@(0.000), @(0.194), @(0.683)];
proPicTranslationXAnimation.keyTimes = @[@(0.000), @(0.500), @(1.000)];
proPicTranslationXAnimation.timingFunctions = @[linearTiming, linearTiming];
proPicTranslationXAnimation.beginTime = beginTime;
proPicTranslationXAnimation.fillMode = fillMode;
proPicTranslationXAnimation.removedOnCompletion = removedOnCompletion;
[[self layer] addAnimation:proPicTranslationXAnimation forKey:@"alertPopUp_TranslationX"];
CAKeyframeAnimation *proPicTranslationYAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
proPicTranslationYAnimation.duration = 0.200;
proPicTranslationYAnimation.values = @[@(0.000), @(11.242), @(0.581)];
proPicTranslationYAnimation.keyTimes = @[@(0.000), @(0.500), @(1.000)];
proPicTranslationYAnimation.timingFunctions = @[linearTiming, linearTiming];
proPicTranslationYAnimation.beginTime = beginTime;
proPicTranslationYAnimation.fillMode = fillMode;
proPicTranslationYAnimation.removedOnCompletion = removedOnCompletion;
[[self layer] addAnimation:proPicTranslationYAnimation forKey:@"alertPopUp_TranslationY"];
}
- (void)addAlertPopUpAnimationWithCompletion:(void (^)(BOOL finished))completionBlock
{
[self addAlertPopUpAnimationWithBeginTime:0 andFillMode:kCAFillModeBoth andRemoveOnCompletion:NO completion:completionBlock];
}
When you want to add animations, call right after you add your view as subview:
You need to subclass the UIView and put the code in there.
Later you can call:
[_yourView addAlertPopUpAnimationWithCompletion:^(BOOL finished) {
//do your stuff after animation is finished
}];
The animation looks like this: