与UIAlertView中添加子视图喜欢动漫,有弹跳效果(Add subview with UIAl

2019-07-03 14:49发布

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

谢谢

Answer 1:

Animation的弹出框的形式:

迅速

    yourView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
    UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {() -> Void in
        yourView.transform = .identity
    }, completion: {(finished: Bool) -> Void in
        // do something once the animation finishes, put it here
    })

Reverse Animationhidingsame View

    yourView.transform = .identity
    UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {() -> Void in
        yourView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
    }, completion: {(finished: Bool) -> Void in
        // do something once the animation finishes, put it here
        yourView.isHidden = 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 Animationhidingsame 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;
}];


Answer 2:

我已经做过......你可以从这个代码让我的想法。 这可能会帮助你。 如果你有任何问题,请让我know.thanks

- (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];
}


Answer 3:

试试这个:

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"];


Answer 4:

试试这三个methodes ..

**** vwTemp是我custome警报视图****

- (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];
}


Answer 5:

    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];

弹跳动画两个时间后使用该波纹管两种方法..

- (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];
}


Answer 6:

使用下面的代码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()}
    )
}


文章来源: Add subview with UIAlertVIew like animation,with a bouncing effect