IOS:如何检测动画正在进行中

时间:2014-01-24 05:51:48

标签: ios objective-c animation uiimageview

以前曾经问过,但没有一个答案能够直截了当地帮助我。

UIIimage视图中.isAnimating到底有什么作用?以及如何正确使用它?

我正在模仿水龙头将动画聚焦在iPhone相机中(弹出黄色方块,然后在点击预览时收缩)。这工作正常,但我希望它不会发生多次,如果它已经动画了。此代码执行动画,但多次点击可提供多个动画方块。我怀疑它不仅仅是.isAnimating而且我还做了别的错误,因为我也尝试了我自己的布尔值而且失败了。

-(void)focusSquarePopUp:(CGPoint)touchPoint;
{

    UIImage *focusSquareImage = [UIImage imageNamed:@"yellowFocusSquare"];
    UIImageView *tmpView = [[UIImageView alloc] initWithImage:focusSquareImage];
    if (!(tmpView.isAnimating))
    {
        tmpView.center = touchPoint;
        tmpView.opaque = YES;
        tmpView.alpha = 1.0f;
        [self.view addSubview:tmpView];
        tmpView.hidden = NO;
        // shrink to half size in .3 second
        [UIView animateWithDuration:0.3 delay:0 options:0 animations:^{
        tmpView.transform = CGAffineTransformMakeScale(.5, .5);
        NSLog(@"in animation isAnimating %hhd", tmpView.isAnimating);
        } completion:^(BOOL finished) {
            // Once the animation is completed hide the view for good
            tmpView.hidden = YES;
        }];
    }
    NSLog(@"done animating isAnimating %hhd", tmpView.isAnimating);
    [tmpView release];
}

如果有问题,这个问题有一个可靠的答案,那就太棒了。

编辑 - 这是工作代码。

-(void)focusSquarePopUp:(CGPoint)touchPoint;
{

    if (animationInProgress)
        return;
    animationInProgress = true;
    UIImageView *tmpView = [[UIImageView alloc] initWithImage:sp_ui->focus_square];

    tmpView.center = touchPoint;
    tmpView.opaque = YES;
    tmpView.alpha = 1.0f;
    [self.view addSubview:tmpView];
    tmpView.hidden = NO;
    // shrink to half size in .3 second
    [UIView animateWithDuration:0.3 delay:0 options:0 animations:^{
        tmpView.transform = CGAffineTransformMakeScale(.5, .5);
    } completion:^(BOOL finished) {
    // Once the animation is completed hide the view for good
     tmpView.hidden = YES;
     animationInProgress = false;

    }];

    [tmpView release];
}

2 个答案:

答案 0 :(得分:2)

我怀疑你的代码。点击方法focusSquarePopUp:后,您可以创建tmpViewfocusSquareImage的新实例,并添加自我视图。这就是为什么你发现动画方块的数量等于水龙头的数量。当你创建那些变量的新实例时,确定isAnimating首先你没有值,它进入动画块代码。

为什么不在类的.h文件中创建tmpViewfocusSquareImage的实例?实际上它是变量声明和变量范围的问题。

你的代码应该是这样的, 在动画类中声明实例(即self.view).h文件

 UIImage *focusSquareImage;
 UIImageView *tmpView;

现在在.m文件viewDidLoad方法中,

在viewDidLoad

focusSquareImage = [UIImage imageNamed:@"yellowFocusSquare"];
tmpView = [[UIImageView alloc] initWithImage:focusSquareImage];

你的动画方法实现应该是,

-(void)focusSquarePopUp:(CGPoint)touchPoint;
{
    if (!(tmpView.isAnimating))
    {
        tmpView.center = touchPoint;
        tmpView.opaque = YES;
        tmpView.alpha = 1.0f;
        [self.view addSubview:tmpView];
        tmpView.hidden = NO;
        // shrink to half size in .3 second
        [UIView animateWithDuration:0.3 delay:0 options:0 animations:^{
        tmpView.transform = CGAffineTransformMakeScale(.5, .5);
        NSLog(@"in animation isAnimating %hhd", tmpView.isAnimating);
        } completion:^(BOOL finished) {
            // Once the animation is completed hide the view for good
            tmpView.hidden = YES;
            [tmpView release];
        }];
    }
}

方法实现的另一种选择,

-(void)focusSquarePopUp:(CGPoint)touchPoint;
{
  if(!tmpView){
    focusSquareImage = [UIImage imageNamed:@"yellowFocusSquare"];
    tmpView = [[UIImageView alloc] initWithImage:focusSquareImage];
    if (!(tmpView.isAnimating))
    {
        tmpView.center = touchPoint;
        tmpView.opaque = YES;
        tmpView.alpha = 1.0f;
        [self.view addSubview:tmpView];
        tmpView.hidden = NO;
        // shrink to half size in .3 second
        [UIView animateWithDuration:0.3 delay:0 options:0 animations:^{
        tmpView.transform = CGAffineTransformMakeScale(.5, .5);
        NSLog(@"in animation isAnimating %hhd", tmpView.isAnimating);
        } completion:^(BOOL finished) {
            // Once the animation is completed hide the view for good
            tmpView.hidden = YES;
            [tmpView release];
            tmpView = nil;
        }];
    }
 }
}

答案 1 :(得分:1)

isAnimating是UIImageView属性,它与UIView动画方法无关

当您希望UIImageView在多个图像之间切换时,使用

isAnimating。例如,在以下代码中,UIImageView显示3个图像并在它们之间交替

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

UIImage *image1 = [UIImage imageNamed:@"image1.png"];
UIImage *image2 = [UIImage imageNamed:@"image2.png"];
UIImage *image3 = [UIImage imageNamed:@"image3.png"];

imageView.animationImages = @[image1, image2, image3];
imageView.animationDuration = 1;

NSLog(@"isAnimating %d",imageView.isAnimating); // isAnimating 0
[imageView startAnimating];
NSLog(@"isAnimating %d",imageView.isAnimating); // isAnimating 1

[self.view addSubview:imageView];

您使用UIView animation blocks的情况完全不同,并且要知道其块中的动画是否已完成,您需要添加一个布尔标志实例变量animationFinished并将其设置为{{ 1}}在动画的完成块中