在 iOS 中添加子视图时如何提供弹跳或弹出式动画?我需要类似于UIAlertView
外观风格的东西。我知道有几种方法可以做到这一点:但我更喜欢在[UIView animateWithDuration: ...];
这里使用基本块,所以我可以得到一些关于transform
我应该在这里使用来获得弹出效果的帮助吗?
谢谢
在 iOS 中添加子视图时如何提供弹跳或弹出式动画?我需要类似于UIAlertView
外观风格的东西。我知道有几种方法可以做到这一点:但我更喜欢在[UIView animateWithDuration: ...];
这里使用基本块,所以我可以得到一些关于transform
我应该在这里使用来获得弹出效果的帮助吗?
谢谢
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 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
})
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 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;
}];
我之前已经做过......你可以从这段代码中得到我的想法。这可能会对您有所帮助。如果您有任何问题,请告诉我。谢谢
- (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];
}
试试这三种方法
****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];
}
试试这个:
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"];
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];
}
使用下面的代码在 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()}
)
}