如何使“摇晃”动画 [英] How to make "shaking" animation

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

问题描述

在我的xcode项目中,我有一个UIImage。我想让它从左到右摇动(动画)一点点。我编写了这段代码,但是它没有用。

In my xcode project I have an UIImage. I want to make it shaking (animation) from left to right a little bit. I wrote this code, however, its not working.

-(void)errorShake
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0];



    [lockImage setTransform:CGAffineTransformMakeRotation(-5 * M_PI / 180.0)];
    [lockImage setTransform:CGAffineTransformMakeRotation(-10 * M_PI / 180.0)];
    [lockImage setTransform:CGAffineTransformMakeRotation(-15 * M_PI / 180.0)];
    [lockImage setTransform:CGAffineTransformMakeRotation(-20 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(-15 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(-10 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(-5 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(0 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(5 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(10 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(15 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(20 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(15 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(10 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(5 * M_PI / 180.0)];
     [lockImage setTransform:CGAffineTransformMakeRotation(0 * M_PI / 180.0)];


    [UIView commitAnimations];
}

请告诉我,我怎么能让我的图像震动?

Could you tell me please, how can I make my image shake ?

推荐答案

以下是我用于此效果的代码。

Here's the code I use for that effect.

 -(void)shakeView {

        CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"];
        [shake setDuration:0.1];
        [shake setRepeatCount:2];
        [shake setAutoreverses:YES];
        [shake setFromValue:[NSValue valueWithCGPoint:
                                 CGPointMake(lockImage.center.x - 5,lockImage.center.y)]];
        [shake setToValue:[NSValue valueWithCGPoint:
                               CGPointMake(lockImage.center.x + 5, lockImage.center.y)]];
        [lockImage.layer addAnimation:shake forKey:@"position"];
    }

提到 Jere 。确保包含导入:

As Jere mentioned. Make sure to include the import:

#import <QuartzCore/QuartzCore.h>

您还可以将其添加为 UIView

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

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