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

发布于 2024-08-15 17:52:38 字数 143 浏览 9 评论 0原文

我已经实现了游戏应用程序。我想在其中对图像的某些区域进行抖动效果动画。如何可能,请给我建议。

编辑: 我的确切疑问是: 在我的游戏应用程序中,有一个图像。现在我选择了图像的某个区域框架。现在我正在摇动 iPhone,那么只有框架图像的选定区域必须是动画的。

I have implemented game application.In which i want to animate some area of images on shake effect.How it possible please give me advice.

Edit:
My excatly query is that:
In my game application there is one image.Now i am selected some area frame of image.now i am shaking iphone then only selected area of frame image must be animate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

極樂鬼 2024-08-22 17:52:38

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

  • viewWillAppear
  • viewDidAppear
  • viewWillDisappear
  • viewDidDisappear
  • canBecomeFirstResponder
  • 变为FirstResponder
  • nextResponder

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

  • motionBegan:withEventmotionEnded
  • :withEvent

我喜欢放一个 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];
    }
}

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

Here is a skeleton ... then you can get more detail at the suggested docs. This is real simple-minded but has a randomization and you can instantiate with your own image. I created an image container as a UIViewController and in my main (either your AppDelegate or the RootViewController) you create the instance in viewDidLoad. The methods that have to be overloaded in your AppDelegate, (the code is at the end of the post) so it receives shakes, are:

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

You become a responder in viewWillAppear and you have to enable the device notifications (verified that there is a documented bug) so the motion methods get invoked.

  • motionBegan:withEvent
  • motionEnded:withEvent

I like to put an NSLog(@"%s", FUNCTION); statement in all my first-time written methods, that way I can quickly determine if something is missing to get my events, properly initialized, etc. Its just my style.

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

This "root or main" delegate is a UIViewController. I have left of detail to show its simplicity, but still left my 'trace' code that I find essential for first-time debugging. Also, as an aside, I believe you have to overload motionBegan, even if you only need the motionEnded event. I recall that my app did not work and then I added the motionBegan and it started to call motionEnded. It kinda' makes sense that it would be that way, but I don't have any documentation to back it up. A simple experiment after you get it working can confirm or deny my comment.

- (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];
    }
}

This code does work -- but if I snipped out too much detail, just ask and I will make certain to add enough to help you along. This was a really rewarding task for me and I am excited to share it with someone looking for some help in the same experience.

属性 2024-08-22 17:52:38

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

Have a look at LocateMe example code, one of the first samples in the SDK and use and change the bounce back to centre code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文