animating a circle to grow

2019-08-27 19:00发布

问题:

i am animating an UIImageView with an image of a circle that grows and fades which repeats:

-(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];
}

i call this method in viewDidAppear:

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

    //other code
}

It will animate fine when i load mainView. the issue is i load view2 and dismiss view2, when i come back to mainView, it is not animating anymore.

any ideas why this is happening?

EDIT: the methods are being called:

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

EDIT 2:

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

dismissing view 2:

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

    [self dismissModalViewControllerAnimated:YES];
}

also, i don't stop the animation myself at any point in my code.

回答1:

My guess is that viewDidAppear is not being called, because from the perspective of that view, it never disappears.

Depending on how you are creating and displaying the view2, would this work within view2 when you close it?

[parentViewController viewDidAppear:YES];


回答2:

i re-did some code;

-(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
}

with these changes, it behaves as i want it too.

i think it had something to do with trying to animate the outerCircle a second time (it was already animating from first viewDidAppear, and when viewDidAppear was called a second time, it re-animated an already animating image and just borked it completely).