2013-03-16 32 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

相关问题