android 头部渐隐效果,简单的渐隐渐现动画效果

随意写的小Demo,适合慢慢显示弹出框的效果以及切换两个界面,代码很简单,可是项目中很实用,至少对我帮助很大,特此记录,效果如下:

1852aaa7a717

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.动画渐隐渐现效果

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,设置动画时长即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值