2009-12-17 39 views
2

我已經實現了遊戲應用程序。在這個應用程序中,我想對動畫效果的某些區域進行動畫處理。請問如何給我建議。如何在搖動效果上動畫圖像?

編輯: 我excatly查詢是: 在我的遊戲應用有一個image.Now我正在選擇image.now我正在搖晃iPhone則只有選擇的幀圖像的區域必須是有生命的一些區域框。

+0

看看這個[博客條目(http://www.cimgf.com/2008/02/27/core-animation-tutorial-window-shake-effect/):它左右搖晃一個窗口,但你可以在相同的技術在圖像上。 – 2009-12-17 08:10:48

回答

2

這是一個框架......然後你可以在建議的文檔中獲得更多的細節。這真的很簡單,但有隨機性,你可以用你自己的圖像實例化。我創建了一個圖像容器作爲UIViewController,並在我的主要(你的AppDelegate或RootViewController)中創建viewDidLoad中的實例。那在你的AppDelegate超載的方法,(代碼是在帖子的末尾),所以它接收奶昔,分別是:

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

您成爲viewWillAppear中的響應者,您必須啓用設備通知(驗證是否存在已記錄的錯誤),以便調用運動方法。

  • motionBegan:withEvent
  • motionEnded: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]; 
    } 
} 

此代碼確實有效 - 但如果我剪掉了太多細節,只要問一下,我會確定添加足夠的信息來幫助您。對我來說,這是一項非常有意義的任務,我很高興與有人在同一經歷中尋求幫助的人分享。

0

查看LocateMe示例代碼,SDK中的第一個示例代碼,並使用並更改反彈回中心代碼。