The device freezes when there are a lot of UIView animation running with UILabels using shadows and I press the Home button

↘锁芯ラ 提交于 2019-12-25 01:17:11

问题


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

回答1:


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




回答2:


Shadows don't animate well at all. Disable them during animations and turn them back on once the animation is complete.

I haven't had freezes when animating with shadows, but performance is unacceptably jerky. In my experience, trying to animate anything that has shadows yields unacceptable results.



来源:https://stackoverflow.com/questions/10658675/the-device-freezes-when-there-are-a-lot-of-uiview-animation-running-with-uilabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!