【问题标题】:The device freezes when there are a lot of UIView animation running with UILabels using shadows and I press the Home button当有很多 UIView 动画使用 UILabels 运行时,设备会冻结,我按下 Home 按钮
【发布时间】:2012-05-18 19:30:43
【问题描述】:

我的应用程序出现了一些问题。当我在一个有很多图标(UIViews)的屏幕中发生错误时,每个图标都运行一个动画。这个画面让人想起跳板画面,动画视觉也类似。

所以,如果我按下主页按钮,应用程序不会进入后台,我也无能为力。甚至电源按钮也不起作用。图标继续抖动。

如果我删除对标签创建方法的调用,则不会发生这种情况。

有什么建议吗?

谢谢!

动画方法(提取自 Three20 api):

- (void)wobble {
    static BOOL wobblesLeft = NO;

    if (isEditing)
    {
        CGFloat rotation = (kWobbleRadians * M_PI) / 180.0;
        CGAffineTransform wobbleLeft = CGAffineTransformMakeRotation(rotation);
        CGAffineTransform wobbleRight = CGAffineTransformMakeRotation(-rotation);

        [UIView beginAnimations:nil context:nil];

        NSInteger i = 0;
        NSInteger nWobblyButtons = 0;



        for(Icon *ic in iconList)
        {
            ++nWobblyButtons;
            i++;
            if (i % 2)
            {
                ic.transform = wobblesLeft ? wobbleRight : wobbleLeft;

            } else {
                ic.transform = wobblesLeft ? wobbleLeft : wobbleRight;
            }
        }


        if (nWobblyButtons >= 1)
        {
            [UIView setAnimationDuration:kWobbleTime];
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDidStopSelector:@selector(wobble)];
            wobblesLeft = !wobblesLeft;

        } else {
            [NSObject cancelPreviousPerformRequestsWithTarget:self];
            [self performSelector:@selector(wobble) withObject:nil afterDelay:kWobbleTime];
        }

        [UIView commitAnimations];
    }
}

标签创建

-(void)layoutLabel
{
    // If title isn`t builded.
    if(_lblName == nil)
    {
        // Create new label.
        _lblName = [[UILabel alloc] initWithFrame:CGRectMake(LABEL_POS_X,
                                                             LABEL_POS_Y,
                                                             LABEL_WIDTH,
                                                             LABEL_HEIGHT)];

        // Clear the background.
        [_lblName setBackgroundColor:[UIColor clearColor]];

        // Sets the font.
        [_lblName setFont:[UIFont fontWithName:@"Helvetica-Bold" size:11.3]];
        [_lblName setAdjustsFontSizeToFitWidth:NO];

        // Sets text color
        [_lblName setTextColor:[UIColor whiteColor]];

        // Adjust the number of lines.
        [_lblName setNumberOfLines:2];

        // Adjust the aligment to center.
        [_lblName setTextAlignment:UITextAlignmentCenter];

        // Adjust shadow like the springboad`s icons.
        _lblName.layer.shadowOpacity = 1.0;
        _lblName.layer.shadowRadius = 0.8;
        _lblName.layer.shadowColor = [[UIColor blackColor] CGColor];
        _lblName.layer.shadowOffset = CGSizeMake(0, 1.2);

        // Add label to container.
        [self addSubview:_lblName];
    }
}

【问题讨论】:

    标签: ios core-animation freeze springboard


    【解决方案1】:

    问题是:

    对于每个图标,我有很多子视图,因此,每个图标在动画时的计算会导致代码变慢。

    所以,我找到了解决这个问题的方法。我构建了相同的图标结构,但之后,我将 UIImage 中的所有内容与下面的代码合并。

    + (UIImage *) imageWithView:(UIView *)view
    {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return img;
    }
    

    这将我的 FPS 从 ~15 提高到 60 O.o

    按照我找到的一些帮助 Link

    【讨论】:

      【解决方案2】:

      阴影的动画效果并不好。在动画期间禁用它们并在动画完成后重新打开它们。

      我在使用阴影制作动画时没有出现冻结,但性能生涩令人无法接受。根据我的经验,尝试为任何有阴影的东西制作动画会产生不可接受的结果。

      【讨论】:

      • 真的,阴影让我的代码变慢了太多。但是,我需要使用阴影,是我设计团队的命令哈哈哈谢谢
      猜你喜欢
      • 1970-01-01
      • 2013-01-30
      • 2018-08-24
      • 2020-11-14
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      相关资源
      最近更新 更多