2

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

编辑:我的异常查询是:在我的游戏应用程序中有一个图像。现在我选择了图像的某个区域框架。现在我正在摇晃 iphone,那么只有框架图像的选定区域必须是动画的。

4

2 回答 2

2

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

  • 视图将出现
  • viewDidAppear
  • 视图将消失
  • viewDidDisappear
  • 可以成为第一响应者
  • 成为第一响应者
  • 下一个响应者

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

  • motionBegan:withEvent
  • 运动结束: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;

动画图像.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

这个“根或主”委托是一个 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];
    }
}

这段代码确实有效——但如果我剪掉了太多细节,请询问,我会确保添加足够的内容来帮助你。这对我来说是一项非常有意义的任务,我很高兴能与在相同经历中寻求帮助的人分享它。

于 2009-12-19T05:14:03.857 回答
0

查看 LocateMe 示例代码,这是 SDK 中的第一个示例,并使用并将反弹更改为中心代码。

于 2009-12-17T22:11:35.773 回答