我已经实现了游戏应用程序。在这个应用程序中,我想对动画效果的某些区域进行动画处理。请问如何给我建议。如何在摇动效果上动画图像?
编辑: 我excatly查询是: 在我的游戏应用有一个image.Now我正在选择image.now我正在摇晃iPhone则只有选择的帧图像的区域必须是有生命的一些区域框。
我已经实现了游戏应用程序。在这个应用程序中,我想对动画效果的某些区域进行动画处理。请问如何给我建议。如何在摇动效果上动画图像?
编辑: 我excatly查询是: 在我的游戏应用有一个image.Now我正在选择image.now我正在摇晃iPhone则只有选择的帧图像的区域必须是有生命的一些区域框。
这是一个框架......然后你可以在建议的文档中获得更多的细节。这真的很简单,但有随机性,你可以用你自己的图像实例化。我创建了一个图像容器作为UIViewController,并在我的主要(你的AppDelegate或RootViewController)中创建viewDidLoad中的实例。那在你的AppDelegate超载的方法,(代码是在帖子的末尾),所以它接收奶昔,分别是:
您成为viewWillAppear中的响应者,您必须启用设备通知(验证是否存在已记录的错误),以便调用运动方法。
我喜欢把一个的NSLog(@ “%s” 时,FUNCTION);声明在我所有的第一次写的方法,这样我可以快速确定是否有什么东西是缺少让我的事件,正确初始化等。它只是我的风格。
AnimatedImage.h ISA的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
这种 “根或主” 代表是一个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];
}
}
此代码确实有效 - 但如果我剪掉了太多细节,只要问一下,我会确定添加足够的信息来帮助您。对我来说,这是一项非常有意义的任务,我很高兴与有人在同一经历中寻求帮助的人分享。
查看LocateMe示例代码,SDK中的第一个示例代码,并使用并更改反弹回中心代码。
看看这个[博客条目(http://www.cimgf.com/2008/02/27/core-animation-tutorial-window-shake-effect/):它左右摇晃一个窗口,但你可以在相同的技术在图像上。 – 2009-12-17 08:10:48