用animation添加UIAlertVIew子视图,具有弹跳效果

如何在iOS中添加子视图时popup或popup风格的animation? 我需要类似于UIAlertView外观样式的东西。 我知道有几种方法可以做同样的事情,但我更喜欢使用基本的[UIView animateWithDuration: ...]; 在这里阻止,所以我可以得到一些关于我应该在这里使用popup效果的transform帮助?

谢谢

PopUp风格的Animation

迅速

 yourView.transform = CGAffineTransformMakeScale(0.01, 0.01) UIView.animateWithDuration(0.2, delay: 0, options: .CurveEaseOut, animations: {() -> Void in yourView.transform = CGAffineTransformIdentity }, completion: {(finished: Bool) -> Void in // do something once the animation finishes, put it here }) 

Reverse Animationhiding same View

 yourView.transform = CGAffineTransformIdentity UIView.animateWithDuration(0.2, delay: 0, options: .CurveEaseOut, animations: {() -> Void in yourView.transform = CGAffineTransformMakeScale(0.01, 0.01) }, completion: {(finished: Bool) -> Void in // do something once the animation finishes, put it here yourView.hidden = true }) 

Objective-C的

 yourView.transform = CGAffineTransformMakeScale(0.01, 0.01); [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ yourView.transform = CGAffineTransformIdentity; } completion:^(BOOL finished){ // do something once the animation finishes, put it here }]; 

Reverse Animationhiding same View

  yourView.transform = CGAffineTransformIdentity; [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ yourView.transform = CGAffineTransformMakeScale(0.01, 0.01); } completion:^(BOOL finished){ yourView.hidden = YES; }]; 

我已经完成了…你可以从这个代码中得到我的想法。 这可能会帮助你。 如果您有任何问题,请告诉我。谢谢

 - (CGAffineTransform)transformForOrientation { UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (orientation == UIInterfaceOrientationLandscapeLeft) { return CGAffineTransformMakeRotation(M_PI*1.5); } else if (orientation == UIInterfaceOrientationLandscapeRight) { return CGAffineTransformMakeRotation(M_PI/2); } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { return CGAffineTransformMakeRotation(-M_PI); } else { return CGAffineTransformIdentity; } } -(void)showCustomAlertView{ UIWindow *window = [UIApplication sharedApplication].keyWindow; //self (this view pop up like alert) [window addSubview:self]; self.transform = CGAffineTransformScale([self transformForOrientation], 0.001, 0.001); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/1.5]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(bounce1AnimationStopped)]; self.transform = CGAffineTransformScale([self transformForOrientation], 1.1, 1.1); [UIView commitAnimations]; } - (void)bounce1AnimationStopped { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/2]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(bounce2AnimationStopped)]; self.transform = CGAffineTransformScale([self transformForOrientation], 0.9, 0.9); [UIView commitAnimations]; } - (void)bounce2AnimationStopped { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/2]; self.transform = [self transformForOrientation]; [UIView commitAnimations]; } 

尝试这个:

 self.popUpContainerView.alpha = 0; [UIView animateWithDuration:0.1 animations:^{self.popUpContainerView.alpha = 1.0;}]; CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"]; bounceAnimation.values = @[@0.01f, @1.1f, @1.0f]; bounceAnimation.keyTimes = @[@0.0f, @0.5f, @1.0f]; bounceAnimation.duration = 0.2; [self.popUpContainerView.layer addAnimation:bounceAnimation forKey:@"bounce"]; 

试试这三种方法

**** vwTemp是我的客户警报视图****

 - (void)showAlertView { vwTemp.transform = CGAffineTransformMakeScale( 0.001, 0.001); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/1.5]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(bounce1AnimationStopped)]; vwTemp.transform = CGAffineTransformMakeScale( 1.1, 1.1); [UIView commitAnimations]; } 

  - (void)bounce1AnimationStopped { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/2]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(bounce2AnimationStopped)]; vwTemp.transform = CGAffineTransformMakeScale (0.9, 0.9); [UIView commitAnimations]; } - (void)bounce2AnimationStopped { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/2]; [UIView setAnimationDelegate:self]; vwTemp.transform = CGAffineTransformIdentity; [UIView commitAnimations]; } 
  yourView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3/1.5]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(bounce1AnimationStopped)]; [self.view addSubview:yourView]; yourView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1); [UIView commitAnimations]; 

在弹跳animation两次之后,这两个方法被使用。

 - (void)bounce1AnimationStopped { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3/2]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(bounce2AnimationStopped)]; yourView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9); [UIView commitAnimations]; } - (void)bounce2AnimationStopped { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3/2]; yourView.transform = CGAffineTransformIdentity; [UIView commitAnimations]; } 

使用下面的代码在swift3中工作

 func popIn(yourView : UIView){ yourView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01) UIView.animateKeyframes(withDuration: 0.2, delay: 0.0, options: UIViewKeyframeAnimationOptions.calculationModeDiscrete, animations: { yourView.transform = .identity }, completion: nil) } func popOut(yourView: UIView) { yourView.transform = .identity UIView.animateKeyframes(withDuration: 0.2, delay: 0.0, options: UIViewKeyframeAnimationOptions.calculationModeDiscrete, animations: { yourView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01) }, completion:{(_ finish : Bool) in yourView.removeFromSuperview()} ) }