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

Posted 稻草人11223

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

效果如下:

 

以上是关于ios开发之--简单动画效果的添加的主要内容,如果未能解决你的问题,请参考以下文章

ios开发之--CAKeyframeAnimation的详细用法

iOS 力学动画生成器UIKit Dynamics 之碰撞效果解说

自己定义View时,用到Paint Canvas的一些温故,简单的帧动画(动画一 ,&quot;掏粪男孩Gif&quot;顺便再提提onWindowFocusChanged)(代码片段

iOS之UIView动画

ios之核心动画(Core Animation)

iOS 动画方法内容整理