如何在震动效果上制作动画图像?

时间:2009-12-17 07:57:29

标签: iphone effect

我已经实现了游戏应用程序。其中我想在震动效果上为某些区域的图像设置动画。如何可能请给我建议。

编辑: 我的疑问是: 在我的游戏应用程序中有一个图像。现在我选择了一些区域的图像框架。现在我正在摇动iphone,然后只有选定的帧图像区域必须是动画。

2 个答案:

答案 0 :(得分:2)

这是一个骨架......然后你可以在建议的文档中获得更多细节。这是真正的简单但有随机化,您可以使用自己的图像进行实例化。我创建了一个图像容器作为UIViewController,并在我的主(您的AppDelegate或RootViewController)中创建了viewDidLoad中的实例。必须在AppDelegate中重载的方法(代码在帖子的末尾),以便它接收到震动,是:

  • viewWillAppear中
  • viewDidAppear
  • viewWillDisappear
  • viewDidDisappear
  • canBecomeFirstResponder
  • becomeFirstResponder
  • nextResponder

您成为viewWillAppear中的响应者,您必须启用设备通知(验证存在记录的错误),以便调用运动方法。

  • motionBegan:withEvent
  • motionEnded:withEvent

我想放一个NSLog(@“%s”, FUNCTION );在我所有的第一次写作方法中的陈述,这样我就可以快速确定是否缺少某些东西来获取我的事件,正确初始化等等。这只是我的风格。

AnimatedImage.h是一个ViewController

    @interface AnimatedImage : UIViewController {

    UIImageView     *image;
    UILabel         *label;

    float cx;
    float cy;
    float duration;
    float repeat;


}

@property (nonatomic, retain) UIImageView *image;
@property (nonatomic, retain) UILabel *label;

-(id) initWithImageName: (NSString*) imageName;

-(IBAction) shake;

AnimatedImage.m

    #include <stdlib.h>
#define random() (arc4random() % ((unsigned)RAND_MAX + 1))

#import "AnimatedImage.h"


@implementation AnimatedImage

@synthesize image;
@synthesize label;


-(id) initWithImageName: (NSString*) imageName {
    NSLog(@"%s", __FUNCTION__);

    image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];

    return self;             
}


-(IBAction) shake {

    cx = 0.90 + (random() % 10) / 100; // cx = 0.95;
    cy = 0.90 + (random() % 10) / 100; // cy = 0.95;
    duration = ( 5.0 + random() % 10) / 1000.0;
    repeat   = (65.0 + random() % 20); 

    image.transform = CGAffineTransformScale(CGAffineTransformIdentity, cx, cy);

    [UIView beginAnimations: @"identifier" context: @"shake"];
    [UIView setAnimationDelegate:image.superclass];
    [UIView setAnimationDuration: duration]; // 0.008];
    [UIView setAnimationRepeatCount: repeat]; // 85.0];
    [UIView setAnimationRepeatAutoreverses: YES];

    image.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);

    [UIView commitAnimations];
}


-(IBAction) bounce {

    image.transform = CGAffineTransformTranslate(image.transform, -20, -6);

    [UIView beginAnimations: @"identifier" context: @"bounce"];
    [UIView setAnimationDelegate:image.superclass];
    [UIView setAnimationDuration: duration];
    [UIView setAnimationRepeatCount: repeat];
    [UIView setAnimationRepeatAutoreverses: YES];

    image.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);

    [UIView commitAnimations];
}


@end

这个“root或main”委托是一个UIViewController。我留下了一些细节来展示它的简洁性,但仍留下我认为对于首次调试至关重要的“跟踪”代码。此外,顺便说一句,我相信你必须重载motionBegan,即使你只需要motionEnded事件。我记得我的应用程序没有用,然后我添加了motionBegan,它开始调用motionEnded。它有点意义,但它我没有任何文件支持它。让它工作后的一个简单实验可以确认或否认我的评论。

- (void)viewDidLoad {
    [super viewDidLoad];
    [super becomeFirstResponder];
    .
    .
    .
    NSLog(@"%s", __FUNCTION__);
    NSLog(@"%s: I %s first responder! (%@)", __FUNCTION__, [self isFirstResponder] ? "am" : "am not", self);
    .
    .<created image in this ViewController's NIB using IB>
    .
    someImage   = (UIImageView *) [self.view viewWithTag:TAG_SOMEIMAGE];
    aniImage = [[AnimatedImage alloc] init];
    aniImage.image = someImage;
}


-(void) viewWillAppear: (BOOL) animated{
    NSLog(@"%s", __FUNCTION__);
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver: self 
                                             selector: @selector(receivedRotate:) 
                                                 name: UIDeviceOrientationDidChangeNotification 
                                               object: nil];
}



- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"%s", __FUNCTION__);
    [super becomeFirstResponder];
    assert( [self canPerformAction:@selector(motionEnded:withEvent:) withSender:self] ); 
}


- (void)viewWillDisappear:(BOOL)animated {
    NSLog(@"%s", __FUNCTION__);
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver: self];
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"%s", __FUNCTION__);

    [super resignFirstResponder];
}


- (BOOL)canBecomeFirstResponder {
    NSLog(@"%s", __FUNCTION__);
    return YES;
}

- (BOOL)becomeFirstResponder {
    NSLog(@"%s", __FUNCTION__);
    return YES;
}

- (UIResponder *)nextResponder {
    NSLog(@"%s", __FUNCTION__);

    return [self.view superview];
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"%s", __FUNCTION__); 
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"%s", __FUNCTION__);

    if( [self isFirstResponder] ) {
        [aniImage shake];
    }
}

这段代码确实有用 - 但是如果我剪掉太多细节,只要问一下,我会确保添加足够的东西来帮助你。这对我来说是一项非常有益的任务,我很高兴与寻求相同经验的人分享。

答案 1 :(得分:0)

查看LocateMe示例代码,SDK中的第一个示例之一,并使用并更改退回到中心代码。