随意写的小Demo,适合慢慢显示弹出框的效果以及切换两个界面,代码很简单,可是项目中很实用,至少对我帮助很大,特此记录,效果如下:
代码如下:
#import "AnimationViewController.h"
@interface AnimationViewController ()
{
UIView *view1;
UIButton *btn;
UIView *view2;
UIButton *btn2;
}
@end
@implementation AnimationViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"动画效果";
//1.动画渐隐渐现效果
view1 = [[UIView alloc] initWithFrame:CGRectMake(30,74 , self.view.frame.size.width-60, 80)];
view1.backgroundColor = [UIColor infoBlueColor];
view1.alpha = 1;
[self.view addSubview:view1];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = 1;
[btn setTitle:@"渐隐/渐现" forState:UIControlStateNormal];
[btn setTitle:@"渐隐/渐现" forState:UIControlStateSelected];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(70, CGRectGetMaxY(view1.frame)+20, self.view.frame.size.width-140, 30);
[self.view addSubview:btn];
//2.两个view切换
view2 = [[UIView alloc] initWithFrame:CGRectMake(30,74 , self.view.frame.size.width-60, 80)];
view2.backgroundColor = [UIColor redColor];
view2.alpha = 0;
[self.view addSubview:view2];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.tag = 2;
[btn2 setTitle:@"切换" forState:UIControlStateNormal];
[btn2 setTitle:@"切换" forState:UIControlStateSelected];
[btn2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[btn2 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
btn2.frame = CGRectMake(70, CGRectGetMaxY(btn.frame)+20, self.view.frame.size.width-140, 30);
[self.view addSubview:btn2];
}
- (void)btnAction:(UIButton *)sender
{
if (sender.tag == 1)
{
if (!btn.selected)
{
[UIView animateWithDuration:1 animations:^{
view1.alpha = 0;
} completion:^(BOOL finished) {
[sender setSelected:YES];
}];
}
else
{
[UIView animateWithDuration:1 animations:^{
view1.alpha = 1;
} completion:^(BOOL finished) {
[sender setSelected:NO];
}];
}
}
else
{
if (!btn2.selected)
{
[UIView animateWithDuration:0.5 animations:^{
view2.alpha = 1;
} completion:^(BOOL finished) {
[UIView transitionFromView:view1 toView:view2 duration:1 options:UIViewAnimationOptionTransitionCurlUp completion:^(BOOL finished) {
[sender setSelected:YES];
}];
}];
}
else
{
[UIView transitionFromView:view2 toView:view1 duration:1 options:UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished) {
[sender setSelected:NO];
}];
}
}
}
Tip
view的渐隐渐现就是设置view的alpha,设置动画时长即可。