美文网首页疯-UI
登录界面输入框抖动动画

登录界面输入框抖动动画

作者: 再见远洋 | 来源:发表于2016-07-19 10:10 被阅读298次

    好吧,我不是很喜欢说废话的人,下面就直接看代码,代码都有详细的注释

    /**
     *  账户名为空或者密码为空时的抖动动画
     *
     *  @param textField 用户名输入框/密码输入框
     */
    - (void)shakeActionWithTextField:(UITextField *)textField
    {
        // 晃动次数
        static int numberOfShakes = 4;
        // 晃动幅度(相对于总宽度)
        static float vigourOfShake = 0.01f;
        // 晃动延续时常(秒)
        static float durationOfShake = 0.5f;
        //抖动动画
        CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
     
        CGRect frame = textField.frame;
        // 创建路径
        CGMutablePathRef shakePath = CGPathCreateMutable();
        // 起始点
        CGPathMoveToPoint(shakePath, NULL, CGRectGetMidX(frame), CGRectGetMidY(frame));
        for (int index = 0; index < numberOfShakes; index++)
        {
            // 添加晃动路径 幅度由大变小
            CGPathAddLineToPoint(shakePath, NULL, CGRectGetMidX(frame) - frame.size.width * vigourOfShake*(1-(float)index/numberOfShakes),CGRectGetMidY(frame));
            CGPathAddLineToPoint(shakePath, NULL,  CGRectGetMidX(frame) + frame.size.width * vigourOfShake*(1-(float)index/numberOfShakes),CGRectGetMidY(frame));
        }
        // 闭合
        CGPathCloseSubpath(shakePath);
        shakeAnimation.path = shakePath;
        shakeAnimation.duration = durationOfShake;
        // 释放
        CFRelease(shakePath);
     //添加动画到输入框layer上--- bingo---
        [textField.layer addAnimation:shakeAnimation forKey:kCATransition];
    }
    

    demo看这里吧:

    https://github.com/wxh794708907/YJYYShakeAnimation/tree/master

    相关文章

      网友评论

        本文标题:登录界面输入框抖动动画

        本文链接:https://www.haomeiwen.com/subject/feemjttx.html