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

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

附一个demo的下载地址:

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

代码如下:

1,准备

BOOL _isOpen;
    NSMutableArray * _btnArray;

2,具体代码

-(void)initUI
{
    _btnArray = [[NSMutableArray alloc]init];
    for (int i=0; i<4; i++)
    {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        btn.frame = CGRectMake(260, 420, 40, 40);
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"c_setting%d",(i+1)%4]] 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 = 0; i < _btnArray.count; i++)
        {
            UIButton * myBtn = [_btnArray objectAtIndex:i];
            [UIView animateWithDuration:0.3
                             animations:^{
                                 myBtn.frame = CGRectMake(190+(i%2)*70, 350+70*(i/2), 40, 40);
                             }
                             completion:^(BOOL finished) {
                                 [UIView animateWithDuration:0.3
                                                  animations:^{
                                                      myBtn.frame = CGRectMake(200+(i%2)*60, 360+(i/2)*60, 40, 40);
                                                  }];
                             }];
        }
        
    }
    else
    {
        //关闭九宫格
        for (int i = 0; i < _btnArray.count; i++)
        {
            UIButton * myBtn = [_btnArray objectAtIndex:i];
            [UIView animateWithDuration:0.3
                             animations:^{
                                 myBtn.frame = CGRectMake(190+(i%2)*70, 350+70*(i/2), 40, 40);
                             }
                             completion:^(BOOL finished) {
                                 [UIView animateWithDuration:0.3
                                                  animations:^{
                                                      myBtn.frame = CGRectMake(260, 420, 40, 40);
                                                      
                                                  }];
                             }];
        }
        
        
    }
    _isOpen = !_isOpen;
}

效果如下:

原文地址:https://www.cnblogs.com/hero11223/p/7468506.html