【问题标题】:animating a circle to grow动画一个圆圈成长
【发布时间】:2012-03-21 00:34:57
【问题描述】:

我正在为 UIImageView 制作动画,其中包含一个不断增长和消退的圆圈图像:

-(void)animateOuterCircle
{
    NSLog(@"animating circles");
    [UIView animateWithDuration:1.5 
                          delay:.5
                        options:(UIViewAnimationOptionAllowUserInteraction)
                     animations:^{
                         self.outerCircle.transform=CGAffineTransformMakeScale(.25, .25);
                     }
                     completion:nil];


    [UIView animateWithDuration:1.5 
                          delay:.5
                        options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction)
                     animations:^{    
                         self.outerCircle.transform=CGAffineTransformMakeScale(1.5, 1.5);
                         self.outerCircle.alpha = 0;
                     }
                     completion:nil];

    [self.view bringSubviewToFront:self.outerCircle];
}

我在 viewDidAppear 中调用了这个方法:

- (void)viewDidAppear:(BOOL)animated
{        
    [self performSelectorOnMainThread:@selector(animateOuterCircle) withObject:nil waitUntilDone:NO];

    //other code
}

当我加载 mainView 时,它的动画效果会很好。问题是我加载 view2 并关闭 view2,当我回到 mainView 时,它不再动画了。

任何想法为什么会发生这种情况?

编辑:正在调用方法:

2012-03-15 14:11:13.946[1529:17903] view2 canceled  //dismissing view2
2012-03-15 14:11:13.947[1529:17903] mapView viewWillAppear fired
2012-03-15 14:11:13.948[1529:17903] mapView viewWillAppear end
2012-03-15 14:11:14.352[1529:17903] mapView viewDidAppear fired
2012-03-15 14:11:14.353[1529:17903] animating circles
2012-03-15 14:11:14.354[1529:17903] mapView viewDidAppear end

编辑 2:

-(IBAction) fourthBtn:(id)sender
{
    view2 *view2 = [self.storyboard instantiateViewControllerWithIdentifier:@"view2"];
    [self presentModalViewController:view2 animated:YES];    
}

关闭视图 2:

-(IBAction) cancel:(id) sender
{    
    NSLog(@"heir canceled");

    [self dismissModalViewControllerAnimated:YES];
}

另外,我自己不会在我的代码中的任何时候停止动画。

【问题讨论】:

  • 也许viewDidAppear 没有被调用?显示视图切换代码。
  • viewDidAppear 是否被调用?自己加载 view2 时是否停止动画?
  • viewDidAppear 确实被调用并且 animateOuterCircle 确实被调用。
  • 您是否使用调试器检查过您对图层的引用是否仍然有效(当动画未出现时)?向nil 发送消息时没有任何反应。

标签: objective-c ios animation


【解决方案1】:

我的猜测是 viewDidAppear 没有被调用,因为从那个视图的角度来看,它永远不会消失。

根据您创建和显示 view2 的方式,当您关闭 view2 时,它会在 view2 中工作吗?

[parentViewController viewDidAppear:YES];

【讨论】:

  • viewDidAppear 被调用并且我的动画方法被调用;它只是不做动画。
  • 你能告诉我们你是如何呈现然后关闭 view2 的吗?
  • 出于好奇 - 尝试更改 outerCircle 的颜色,看看它是否仍然被引用。如果不是,那么它可能没有被正确保留/引用。你是如何创建外圈的?
【解决方案2】:

我重新做了一些代码;

-(void)viewWillDisappear:(BOOL)animated
{
    [self.outerCircle removeFromSuperview];
    [self.innerCircle removeFromSuperview];
}

- (void)viewDidAppear:(BOOL)animated
{
    self.innerCircle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"inner-circle.png"]];
    self.innerCircle.frame = CGRectMake(self.innerCircle.frame.origin.x, self.innerCircle.frame.origin.y, 30, 30);

    self.outerCircle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"outer-circle.png"]];
    self.outerCircle.frame = CGRectMake(self.outerCircle.frame.origin.x, self.outerCircle.frame.origin.y, 40, 40);

    [self.view insertSubview:self.innerCircle belowSubview:HUD];
    [self.view insertSubview:self.outerCircle belowSubview:HUD];

//other code
}

通过这些更改,它的行为也如我所愿。

我认为这与尝试第二次为 outerCircle 设置动画有关(它已经从第一个 viewDidAppear 开始设置动画,并且当第二次调用 viewDidAppear 时,它重新设置了一个已经设置动画的图像并且完全破坏了它)。

【讨论】:

    猜你喜欢
    • 2021-06-24
    • 2019-09-04
    • 2018-01-12
    • 1970-01-01
    • 2023-03-17
    • 2012-11-12
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多