我已经实现了游戏应用程序。在该应用程序中,我想对抖动效果的某些图像区域进行动画处理。

编辑:
我的疑问是:
在我的游戏应用程序中有一个图像。现在我已选择图像的某个区域框架。现在我在摇动iphone,则只有框架图像的所选区域必须是动画。

最佳答案

这是一个框架……然后您可以在建议的文档中获得更多详细信息。这确实很简单,但是具有随机性,您可以使用自己的图像实例化。我创建了一个图像容器作为UIViewController,并在我的主容器(您的AppDelegate或RootViewController)中,在viewDidLoad中创建了实例。必须在您的AppDelegate中重载的方法(代码在文章结尾),这样它才能引起动摇:


viewWillAppear
viewDidAppear
viewWillDisappear
viewDidDisappear
canBecomeFirstResponder
成为第一响应者
nextResponder


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


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;


动画图像

    #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。我已经详细说明了它的简单性,但是仍然保留了我认为对首次调试必不可少的“跟踪”代码。而且,顺便说一句,我相信即使您只需要motionEnded事件,也必须重载motionBegan。我记得我的应用程序无法正常工作,然后我添加了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];
    }
}


这段代码确实有用-但是,如果我省略了太多细节,请问一下,我会确保添加足够的内容来帮助您。对我来说,这是一项非常有意义的任务,我很高兴与寻求相同经验的人分享它。

关于iphone - 如何在抖动效果上对图像进行动画处理?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1920132/

10-14 16:50
查看更多