zoukankan      html  css  js  c++  java
  • iOS UIView简单缩放动画

    @interface ViewController () {
        UIView *animationView;
        UIButton *button;
        CGPoint animationPoint;
    }
    
    @end

    初始化button和动画的view

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        
        // 创建Button
        button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.layer.borderWidth = 0.5f;
        button.layer.cornerRadius = 7.0f;
        button.frame = CGRectMake(240, 50, 60, 25);
        [button setTitle:@"动画" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(showAnimation) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        //  动画缩放开始的点
        animationPoint = CGPointMake(button.frame.origin.x + button.frame.size.width / 2, 0);
        
        //  动画view
        animationView = [[UIView alloc] initWithFrame:CGRectMake(20, button.frame.origin.y + button.frame.size.height + 10, 280, 100)];
        animationView.backgroundColor = [UIColor grayColor];
        animationView.layer.cornerRadius = 7.0f;
        animationView.alpha = 0.0f;
        [self.view addSubview:animationView];
    }

    动画的处理方法

    - (void)showAnimation {
        
        CGFloat offsetX = animationPoint.x - self.view.frame.size.width / 2;
        CGFloat offsetY = animationPoint.y - animationView.frame.size.height / 2;
    
        if (animationView.alpha == 0.0f) {
            // 动画由小变大
            animationView.transform = CGAffineTransformMake(0.01, 0, 0, 0.01, offsetX, offsetY);
            
            [UIView animateWithDuration:0.3f animations:^{
                animationView.alpha = 1.0f;
                animationView.transform = CGAffineTransformMake(1.05f, 0, 0, 1.0f, 0, 0);
                
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.1f animations:^{
                    animationView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
                } completion:^(BOOL finished) {
                    //  恢复原位
                    animationView.transform = CGAffineTransformIdentity;
                }];
            }];
            
        } else {
            
            // 动画由大变小
            animationView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
            [UIView animateWithDuration:0.2 animations:^{
                animationView.transform = CGAffineTransformMake(0.01, 0, 0, 0.01, offsetX, offsetY);
            } completion:^(BOOL finished) {
                animationView.transform = CGAffineTransformIdentity;
                animationView.alpha = 0.0f;
            }];
        }
        
    }

    动画效果图

  • 相关阅读:
    如何判断单链表是否存在环
    语法面试等题目汇总
    数据分析师常见的10道面试题解答
    Python 的函数
    Python 的错误和异常处理
    Python 的条件语句和循环语句
    Python 的基本运算和内置函数
    Python 的数据表示
    Python 基础
    关于 Python
  • 原文地址:https://www.cnblogs.com/wb145230/p/4502930.html
Copyright © 2011-2022 走看看