ios开发之--简单动画效果的添加

时间:2023-03-08 21:22:46

记录一个简单的动画效果,自己写的,很简单,仅做记录。

附一个demo的下载地址:

https://github.com/hgl753951/hglTest.git

代码如下:

1,准备

BOOL _isOpen;
NSMutableArray * _btnArray;

2,具体代码

-(void)initUI
{
_btnArray = [[NSMutableArray alloc]init];
for (int i=; i<; i++)
{
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = i;
btn.frame = CGRectMake(, , , );
[btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"c_setting%d",(i+)%]] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[_btnArray addObject:btn]; }
} -(void)btnClick:(UIButton *)btn
{
//如果没有打开
if (!_isOpen)
{
//打开九宫格
for (int i = ; i < _btnArray.count; i++)
{
UIButton * myBtn = [_btnArray objectAtIndex:i];
[UIView animateWithDuration:0.3
animations:^{
myBtn.frame = CGRectMake(+(i%)*, +*(i/), , );
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.3
animations:^{
myBtn.frame = CGRectMake(+(i%)*, +(i/)*, , );
}];
}];
} }
else
{
//关闭九宫格
for (int i = ; i < _btnArray.count; i++)
{
UIButton * myBtn = [_btnArray objectAtIndex:i];
[UIView animateWithDuration:0.3
animations:^{
myBtn.frame = CGRectMake(+(i%)*, +*(i/), , );
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.3
animations:^{
myBtn.frame = CGRectMake(, , , ); }];
}];
} }
_isOpen = !_isOpen;
}

效果如下:

ios开发之--简单动画效果的添加