0

我有一个包含 UIView 动画的 modelViewController。当动画块完成时,它会调用自己,从而循环。

当我关闭调用 [_starView removeFromSuperview] 的 modelViewController (dismissInfo) 时,该函数被一次又一次地快速调用,NSLog 行每秒打印多次。

@implementation InfoVC
{
    NSArray *imgs;
    NSString *currentImg;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _imageview.contentMode = UIViewContentModeLeft;
    _imageviewUnder.contentMode = UIViewContentModeLeft;

    imgs = [NSArray arrayWithObjects:
            @"01.jpg",
            @"02.jpg",
            @"03.jpg",
            @"04.jpg",
            @"05.jpg",
            @"06.jpg",
            nil];

    _imgInt = (arc4random()%6);

    [self initialImage];
}

- (void)viewWillAppear:(BOOL)animated{
}


- (void)viewDidAppear:(BOOL)animated{
    NSLog(@"viewDidAppear");
}

- (void)initialImage
{        
    _starView.contentMode = UIViewContentModeLeft;
    _imageviewUnder.contentMode = UIViewContentModeLeft;

    currentImg = [imgs objectAtIndex:_imgInt];
    UIImage *image = [UIImage imageNamed:currentImg];

    _starView = [[UIImageView alloc] initWithImage:image];

    // Size the image view to the image (it's bigger)
    _starView.bounds = CGRectMake(0.0, 44.0, 416.0, 416.0);

    NSLog(@"tarView.center %@", NSStringFromCGPoint(_starView.center) );

    _starView.alpha=1;

    int nextImgInt = _imgInt + 1 ;

    if (nextImgInt>5)
    {
        nextImgInt=0;
    }

    NSString *nextImg = [imgs objectAtIndex:nextImgInt];
    UIImage *nextImage = [UIImage imageNamed:nextImg];
    [_imageviewUnder setImage:nextImage];

    [self.view sendSubviewToBack:_imageviewUnder];


    _imgInt++;

    if (_imgInt>5) {
        _imgInt=0;
    }

    [UIView setAnimationBeginsFromCurrentState:YES];

    [UIView animateWithDuration:7.6f
                          delay:0.1f
                        options:UIViewAnimationCurveLinear
                     animations:^{
                         [_starView setCenter:CGPointMake(112, 208)];
                         [_starView setAlpha:0.0f];
                     }
                     completion:^(BOOL finished){
                         [_starView removeFromSuperview];
                         [self initialImage];
                     }];

    [self.view insertSubview:_starView atIndex:1];

}


- (void)visitTwitter
{    
    NSURL *URL = [NSURL URLWithString:@"http://twitter.com/"];
    SVWebViewController *webViewController = [[SVWebViewController alloc] initWithURL:URL];
    [self.navigationController pushViewController:webViewController animated:YES];

}



- (IBAction)dismissInfo:(id)sender
{
    [self cleanup];
    [self dismissModalViewControllerAnimated:YES];
}


- (void)cleanup
{
    [_starView.layer removeAllAnimations];
    [self.view.layer removeAllAnimations];
    [_starView removeFromSuperview];
}

- (void)viewDidUnload
{
    [self cleanup];
    [super viewDidUnload];
}

@end
4

3 回答 3

0

让自己成为一个动画标志,在 viewDidLoad 中设置为 true,在 cleanup 中设置为 false。然后在您的 initialImage 方法中检查它:

if ( ! animating )
    return;
于 2013-02-13T17:16:04.353 回答
0

这是我第二次遇到这种情况。一个可能的原因是在 viewDidLoad 中初始化的方法,但是子视图和图像没有及时准备好(动画循环的第一遍似乎无法向机器添加重影)。

我将调用移至 ViewDidAppear,确保显示初始化图像以停止白色闪烁。最后一个模态窗口中的无限动画

- (void)viewDidLoad
{
    [super viewDidLoad];

    _imageview.contentMode = UIViewContentModeLeft;
    _imageviewUnder.contentMode = UIViewContentModeLeft;

    imgs = [NSArray arrayWithObjects:
            @"01.jpg",
            @"02.jpg",
            @"03.jpg",
            @"04.jpg",
            @"05.jpg",
            @"06.jpg",
            nil];

    _imgInt = (arc4random()%6);

    currentImg = [imgs objectAtIndex:_imgInt];
    UIImage *image = [UIImage imageNamed:currentImg];
    [_imageviewUnder setImage:image];
}

- (void)viewDidAppear:(BOOL)animated{
    [self initialImage];
}

- (void)initialImage
{        
    currentImg = [imgs objectAtIndex:_imgInt];
    UIImage *image = [UIImage imageNamed:currentImg];

    _starView = [[UIImageView alloc] initWithImage:image];

    // Size the image view to the image (it's bigger)
    _starView.bounds = CGRectMake(0.0, 44.0, 416.0, 416.0);

    NSLog(@"initialImage");

    _starView.alpha=1;

    [self.view insertSubview:_starView atIndex:1];

    int nextImgInt = _imgInt + 1 ;

    if (nextImgInt>5)
    {
        nextImgInt=0;
    }

    NSString *nextImg = [imgs objectAtIndex:nextImgInt];
    UIImage *nextImage = [UIImage imageNamed:nextImg];
    [_imageviewUnder setImage:nextImage];

    [self.view sendSubviewToBack:_imageviewUnder];

    _imgInt++;

    if (_imgInt>5) {
        _imgInt=0;
    }

    [UIView setAnimationBeginsFromCurrentState:YES];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:7.6];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    //[UIView setAnimationDidStopSelector:@selector(initialImage) ];
    [_starView setCenter:CGPointMake(112, 208)];
    [_starView setAlpha:0.0f];
    [UIView commitAnimations];

    [self performSelector:@selector(initialImage) withObject:nil afterDelay:7.6];
}

- (IBAction)dismissInfo:(id)sender
{
    [self cleanup];
    [self dismissModalViewControllerAnimated:YES];
}

- (void)cleanup
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [_starView.layer removeAllAnimations];
    [self.view.layer removeAllAnimations];
    [_starView removeFromSuperview];
}
于 2013-02-14T10:33:27.283 回答
0

如果你只想创建一个循环动画,最好的办法是使用选项:' UIViewAnimationOptionRepeat '。例如:

[UIView animateWithDuration:2.0
                      delay:0.0f
                    options:UIViewAnimationOptionRepeat
                 animations:^{
                            [myUIViewThing setCenter:CGPointMake(myUIViewThing.center.x - 100, myUIViewThing.center.y)];
                             }
                 completion:nil];
于 2014-05-06T08:47:12.953 回答