0

我在我的应用程序中遇到了一些问题。当我在一个有很多图标(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];
    }
}
4

2 回答 2

2

问题是:

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

所以,我找到了一些解决这个问题的方法。我构建了相同的图标结构,但是之后,我将 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 Oo

遵循我找到的一些帮助 链接

于 2012-05-21T19:28:08.967 回答
0

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

使用阴影制作动画时我没有冻结,但性能非常不稳定。以我的经验,尝试为任何有阴影的东西制作动画会产生不可接受的结果。

于 2012-05-19T17:36:02.310 回答