• 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;
    }

    效果如下:

  • 相关阅读:
    第八次作业
    设计一款给爸爸妈妈用的手机
    第五次作业
    第四次作业(项目分析)
    第二次作业(个人项目实践)
    即时通讯软件的发展演变
    C++用法的学习心得
    JavaScript(变量、作用域和内存问题)
    一、Java和JavaScript
    使用Hyper-V创建虚拟机
  • 原文地址:https://www.cnblogs.com/hero11223/p/7468506.html
Copyright © 2020-2023  润新知