使用UIAlertVIew添加子视图,如动画,具有弹跳效果

时间:2012-11-06 11:46:29

标签: iphone ios core-animation core-graphics

如何在iOS中添加子视图时提供反弹或弹出式动画? 我需要类似于UIAlertView外观风格的东西。我知道有几种方法可以做同样的事情:但是我更喜欢在这里使用基本的[UIView animateWithDuration: ...];块,所以我可以获得一些关于我应该在这里用来获得弹出效果的transform的帮助吗? / p>

谢谢

6 个答案:

答案 0 :(得分:46)

PopUp风格的

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
    })
{p> Reverse Animation hiding same 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
    })

目标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
}];
{p> Reverse Animation hiding 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;
}];

答案 1 :(得分:2)

我以前做过......你可以从这段代码中得到我的想法。这可能对你有帮助。如果您有任何问题,请告诉我。谢谢

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

答案 2 :(得分:1)

尝试这三种方法..

**** vw Temp是我的自定义提醒视图****

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

答案 3 :(得分:1)

试试这个:

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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