2013-03-16 19 views
3

我目前使用這個核心動畫; (iOS 5中,ARC)CoreAnimation:將動畫對象轉移到下一個UIViewController

- (void)onTimer 
{ 
    // build a view from our image 
    UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage]; 

    // use the random() function to randomize up our flake attributes 
    int startX = round(random() % 320); 
    int endX = startX; //round(random() % 320); 
    double scale = 1/round(random() % 100) + 1.0; 
    double speed = 1/round(random() % 100) + 1.0; 

    // set the flake start position 
    flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale); 
    flakeView.alpha = 0.8; 

    // put the flake in our main view 
    [self.view addSubview:flakeView]; 
    [self.view sendSubviewToBack:flakeView]; 

    [UIView beginAnimations:nil context:(__bridge void*)flakeView]; 
    // set up how fast the flake will fall 
    [UIView setAnimationDuration:10 * speed]; 

    // set the postion where flake will move to 
    flakeView.frame = CGRectMake(endX, 500.0, 25.0 * scale, 25.0 * scale); 

    // set a stop callback so we can cleanup the flake when it reaches the 
    // end of its animation 
    [UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)]; 
    [UIView setAnimationDelegate:self]; 
    [UIView commitAnimations]; 

} 
- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 

    UIImageView *flakeView = (__bridge UIImageView*)context; 
    [flakeView removeFromSuperview]; 
    flakeView = nil; 
} 

火;

[NSTimer scheduledTimerWithTimeInterval:(0.3) target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; 

有了這段代碼,就有10個雪片被呈現,掉下來。

藉助此代碼以及一些自定義視圖轉換,我正在嘗試使導航操作變得流暢。問題是我無法找到正確的方法將這些(動畫)上下文轉移到下一個UIViewController,所以所有的動畫片雪花將繼續保持他們離開的位置,並且下一個UIViewControllerNSTimer將激發新的動畫。

任何幫助和建議將appriciated。

+0

您不清楚您嘗試傳輸的內容。你想把實際的圖像視圖轉移到下一個控制器嗎?描述你正在尋找的視覺效果可能很有用。順便說一句,你根本不應該使用這種動畫方法。您應該在iOS 4或更高版本中使用基於塊的方法。 – rdelmar 2013-03-16 04:58:07

回答

0

也許你可以把你的上下文放在你這個類的私有擴展中。
我喜歡:@property (nonatomic,weak)IBOutlet UIImageView*context;

0

我懷疑你是否可以傳遞一個動畫視圖到另一個視圖控制器。我懷疑當你從它的超級視圖中刪除它時,它會終止任何動畫。

您可能會更好地編寫跟蹤在數組中動畫的視圖的代碼。當需要創建新的視圖控制器時,查詢每個圖像視圖的圖層的presentationLayer並獲取它的位置。然後從當前視圖控制器中刪除所有雪花視圖,並將整個數組以及一系列位置傳遞給新的視圖控制器。然後在新視圖控制器的viewDidLoad中,將雪花視圖數組添加到它們以前的位置,並開始動畫到它們以前的結束位置,並啓動動畫計時器以開始添加更多的雪花。

+0

我創建了一個Singleton(動畫管理器),它的工作完美。我會盡快發佈答案。和在數組上保持動畫對象的+1。乾杯! – Bartu 2013-03-18 00:32:59

+0

另一種選擇是將雪花與其自己的視圖控制器保持在一個單獨的視圖中,然後將雪花容器視圖添加到下一個顯示的視圖中。 – 2013-03-18 00:34:19

+0

經理是navigationController的委託人。所以當導航操作發生時,它只是將動畫對象(從數組)添加到新的視圖控制器。讓我把代碼:) – Bartu 2013-03-18 00:37:07

0

.h文件中

.m文件

+ (id)sharedManager { 
    static BeanManager *sharedMyManager = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     sharedMyManager = [[self alloc] init]; 
    }); 
    return sharedMyManager; 
} 

- (id)init { 
    if (self = [super init]) { 
     coffeebean = [UIImage imageNamed:@"coffeebean"]; 

     // assign self as navigation controllers delegate 
     AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
     [appDelegate.navigationController setDelegate:self]; 

     self.currentViewController = appDelegate.navigationController.visibleViewController; 

     beanDictionary = [[NSMutableDictionary alloc] init]; 

     NSLog(@"%@ : Allocating",[self class]); 

     [self validate]; 
    } 
    return self; 
} 

- (void)dealloc { 
    if (isDebugging) { 
     NSLog(@"%@: Deallocating", [self class]); 
    } 
} 

#pragma mark - UINavigationController 
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{ 
    if (isDebugging) { 
     NSLog(@"%@: Pushing %d beans to new view",[self class], [[beanDictionary allKeys] count]); 
    } 
    for (UIImageView *animatingBeans in [beanDictionary allValues]) { 
     [viewController.view addSubview:animatingBeans]; 
     [viewController.view sendSubviewToBack:animatingBeans]; 
    } 

    self.currentViewController = viewController; 
} 

#pragma mark - Internals 
- (void)validate 
{ 
    beanTimer = [NSTimer scheduledTimerWithTimeInterval:(0.3) target:self selector:@selector(onTimerWithBlock) userInfo:nil repeats:YES]; 
} 

- (void)invalidate 
{ 
    [beanTimer invalidate]; 
} 

- (void)onTimerWithBlock 
{ 
    // build a view from our flake image 
    UIImageView* beanView = [[UIImageView alloc] initWithImage:coffeebean]; 

    // use the random() function to randomize up our flake attributes 
    int startX = round(random() % 320); 
    int endX = startX; //round(random() % 320); 
    double scale = 1/round(random() % 100) + 1.0; 
    double speed = 1/round(random() % 100) + 1.0; 

    // set the flake start position 
    beanView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale); 
    beanView.alpha = 0.8; 
    beanView.tag = contextTag; 

    [beanDictionary setObject:beanView forKey:[NSString stringWithFormat:@"%d",contextTag]]; 

    if (contextTag > 1000) { 
     contextTag = 0; 
    } 
    contextTag++; 

    // put the flake in our main view 
    [self.currentViewController.view addSubview:beanView]; 
    [self.currentViewController.view sendSubviewToBack:beanView]; 

    [UIView animateWithDuration:(speed * 10) animations:^{ 

     [beanView setFrame:CGRectMake(endX, 500.0, 25.0 * scale, 25.0 * scale)]; 

    } completion:^(BOOL finished) 
    { 
     [beanDictionary removeObjectForKey:[NSString stringWithFormat:@"%d",beanView.tag]]; 
     if (isDebugging) { 
      NSLog(@"%@: Dictionary Count %d",[self class], [[beanDictionary allKeys] count]); 
     } 
     [beanView removeFromSuperview]; 
    }]; 
} 

此代碼似乎做的工作。就目前而言,我並沒有將它用作Singleton,但是如果我將擴展這些代碼,它應該作爲一個單例來實現,只是爲了澄清。我使用的是NSDictionary而不是NSArray,它可以像現在這樣工作,再次考慮我可能會添加一些功能,因此我將其設置爲NSDictionary

相關問題