UIView的摇动画 [英] UIView shake animation

查看:129
本文介绍了UIView的摇动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个UIView震动时,一个按钮为pressed。

我适应code我在<找到的href=\"http://www.cimgf.com/2008/02/27/core-animation-tutorial-window-shake-effect/\">http://www.cimgf.com/2008/02/27/core-animation-tutorial-window-shake-effect/.

然而,努力适应以下code撼动一个UIView,这是行不通的:

   - (无效){动画
    const int的numberOfShakes = 8;
    常量浮durationOfShake = 0.5F;
    常量浮vigourOfShake = 0.1F;    CAKeyframeAnimation * shakeAnimation = [CAKeyframeAnimation动画]    的CGRect帧= lockView.frame;    CGMutablePathRef shakePath = CGPathCreateMutable();
    CGPathMoveToPoint(shakePath,NULL,CGRectGetMinX(帧),CGRectGetMinY(帧));    对于(INT指数= 0;指数 - LT; numberOfShakes ++指数){
        CGPathAddLineToPoint(shakePath,NULL,CGRectGetMinX(帧) - frame.size.width * vigourOfShake,CGRectGetMinY(帧));        CGPathAddLineToPoint(shakePath,NULL,CGRectGetMinX(架)+ frame.size.width * vigourOfShake,CGRectGetMinY(帧));
    }    CGPathCloseSubpath(shakePath);    shakeAnimation.path = shakePath;
    shakeAnimation.duration = durationOfShake;
    [lockView.layer addAnimation:shakeAnimation forKey:@frameOrigin];}


解决方案

我写的文章。这是矫枉过正一个UIView,加上参数都朝着OSX应用为目标。做到这一点吧。

  CABasicAnimation *动画=
                         [CABasicAnimation animationWithKeyPath:@位置];
[动画setDuration:0.05];
[动画setRepeatCount:8];
[动画setAutoreverses:YES];
[动画setFromValue:[NSValue valueWithCGPoint:
               CGPointMake([lockView中心] .X - 20.0f [lockView中心] .Y)];
[动画setToValue:[NSValue valueWithCGPoint:
               CGPointMake([lockView中心] .X + 20.0f [lockView中心] .Y)];
[lockView层] addAnimation:动画forKey:@位置];

您将有时间和的repeatCount参数以及从和值在中心x距离打,但它应该给你你需要什么。我希望帮助。让我知道如果你有任何问题。

i'm trying to make a UIView shake when a button is pressed.

I am adapting the code I found on http://www.cimgf.com/2008/02/27/core-animation-tutorial-window-shake-effect/.

However, by trying to adapt the following code to shake a UIView, it does not work:

- (void)animate {
    const int numberOfShakes = 8;
    const float durationOfShake = 0.5f;
    const float vigourOfShake = 0.1f;

    CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animation];

    CGRect frame = lockView.frame;

    CGMutablePathRef shakePath = CGPathCreateMutable();
    CGPathMoveToPoint(shakePath, NULL, CGRectGetMinX(frame), CGRectGetMinY(frame));

    for (int index = 0; index < numberOfShakes; ++index) {
        CGPathAddLineToPoint(shakePath, NULL, CGRectGetMinX(frame) - frame.size.width * vigourOfShake, CGRectGetMinY(frame));

        CGPathAddLineToPoint(shakePath, NULL, CGRectGetMinX(frame) + frame.size.width * vigourOfShake, CGRectGetMinY(frame));
    }

    CGPathCloseSubpath(shakePath);

    shakeAnimation.path = shakePath;
    shakeAnimation.duration = durationOfShake;


    [lockView.layer addAnimation:shakeAnimation forKey:@"frameOrigin"];

}

解决方案

I wrote that post. It's overkill for a UIView, plus the parameters are geared toward an OSX app. Do this instead.

CABasicAnimation *animation = 
                         [CABasicAnimation animationWithKeyPath:@"position"];
[animation setDuration:0.05];
[animation setRepeatCount:8];
[animation setAutoreverses:YES];
[animation setFromValue:[NSValue valueWithCGPoint:
               CGPointMake([lockView center].x - 20.0f, [lockView center].y)]];
[animation setToValue:[NSValue valueWithCGPoint:
               CGPointMake([lockView center].x + 20.0f, [lockView center].y)]];
[[lockView layer] addAnimation:animation forKey:@"position"];

You'll have to play with the duration and repeatCount parameters as well as the x distance from center in the from and to values, but it should give you what you need. I hope that helps. Let me know if you have any questions.

这篇关于UIView的摇动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆