隨意寫的小Demo,適合慢慢顯示彈出框的效果以及切換兩個界面,代碼很簡單老虫,可是項目中很實用叶骨,至少對我?guī)椭艽螅卮擞涗浧沓祝Ч缦拢?/p>
demo.gif
代碼如下:
#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.動畫漸隱漸現(xiàn)效果
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:@"漸隱/漸現(xiàn)" forState:UIControlStateNormal];
[btn setTitle:@"漸隱/漸現(xiàn)" 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的漸隱漸現(xiàn)就是設置view的alpha忽刽,設置動畫時長即可。