当使用阴影的UILabel运行大量UIView动画时,设备会冻结,然后按“主页”按钮 [英] The device freezes when there are a lot of UIView animation running with UILabels using shadows and I press the Home button

查看:87
本文介绍了当使用阴影的UILabel运行大量UIView动画时,设备会冻结,然后按“主页”按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序出现问题。当我在一个带有很多图标(UIViews)的屏幕中,并且每个图标都运行一个动画时,就会发生错误。该屏幕使人联想起跳板屏幕,动画效果也相似。什么也没做。甚至电源按钮也不起作用。并且图标继续晃动。



如果我取消了标签创建方法的调用,就不会发生。



有任何建议吗?



谢谢!



动画方法(从Three20 api中提取):

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

如果(isEditing)
{
CGFloat旋转=(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;

}否则{
ic.transform = wobblesLeft吗? wobbleLeft:wobbleRight;
}
}


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

}否则{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self performSelector:@selector(wobble)withObject:n after afterlay:kWobbleTime];
}

[UIView commitAnimations];
}
}

标签创建

 -(void)layoutLabel 
{
//如果未构建标题。
if(_lblName == nil)
{
//创建新标签。
_lblName = [[UILabel alloc] initWithFrame:CGRectMake(LABEL_POS_X,
LABEL_POS_Y,
LABEL_WIDTH,
LABEL_HEIGHT)];

//清除背景。
[_lblName setBackgroundColor:[UIColor clearColor]];

//设置字体。
[_lblName setFont:[UIFont fontWithName:@ Helvetica-Bold size:11.3]]];
[_lblName setAdjustsFontSizeToFitWidth:NO];

//设置文本颜色
[_lblName setTextColor:[UIColor whiteColor]];

//调整行数。
[_lblName setNumberOfLines:2];

//调整对中位置。
[_lblName setTextAlignment:UITextAlignmentCenter];

//像springboad的图标一样调整阴影。
_lblName.layer.shadowOpacity = 1.0;
_lblName.layer.shadowRadius = 0.8;
_lblName.layer.shadowColor = [[UIColor blackColor] CGColor];
_lblName.layer.shadowOffset = CGSizeMake(0,1.2);

//将标签添加到容器。
[self addSubview:_lblName];
}
}


解决方案

问题是:



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



因此,我发现了一些解决方案。我建立了相同的图标结构,但是之后,我在下面的代码中合并了UIImage中的所有内容。

  +(UIImage * )imageWithView:(UIView *)view 
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size,view.opaque,0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

返回img;
}

这使我的FPS从约15增至60 Oo



遵循一些我发现的帮助
链接


I'm having some problems in my application. The error occurs when I'm in a screen with a lot of icons (UIViews), each one running a animation. This screen reminds the springboard screen, and the animation visual is similar too.

So, if I press the home button, the application doesn't goes to background and I can't do anything. Even the power button doesn't work. And the icons continue shaking.

If I remove the call for label creation method, this don't happens.

Any advice?

Thanks!

Animation method (extracted from 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];
    }
}

Label creation

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

解决方案

The problem is:

For each icon I have many subviews, so, the calculations of each icon, at animation time, resulted in a slow code.

So, I found some light to solve this. I builded the same icon structure, but, after, I merged everithing inside a UIImage with a code below.

+ (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;
}

This increased my FPS from ~15 to 60 O.o

Follow some help that I found Link

这篇关于当使用阴影的UILabel运行大量UIView动画时,设备会冻结,然后按“主页”按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆