【问题标题】:Why do my UIView animations work okay in iOS 5 but no iOS 4?为什么我的 UIView 动画在 iOS 5 中可以正常工作,但在 iOS 4 中不行?
【发布时间】:2011-10-31 13:02:22
【问题描述】:

我有一个自定义 UIView,其中包含 UIImageView、UILabel 和 UIButton 类型的嵌套视图。视图是用这样的代码创建的:

- (void)setupView
{
    popupView = [[UIView alloc] initWithFrame:CGRectMake(-116, -61, 247, 59)];
    popupView.autoresizingMask = 63;
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bubble.png"]];
    bgImageView.autoresizingMask = 63;
    [popupView addSubview:bgImageView];
    [bgImageView release];

    textLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 8, 180, 20)];
    textLabel.autoresizingMask = 63;
    textLabel.font = [UIFont fontWithName:@"DefaultNormal" size:16];
    textLabel.text = @"DYNAMIC TEXT";
    [popupView addSubview:textLabel];

    popupButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    [rightAccessoryButton setImage:[UIImage imageNamed:@"btn-tick.png"] forState:UIControlStateNormal];
    popupButton.frame = CGRectMake(208, 8, 32, 32);
    popupButton.autoresizingMask = 63;
    [popupView addSubview:rightAccessoryButton];
}

我已将他们所有的autoresizingMasks 设置为 63,这基本上是所有内容都处于 ON 位置(除了将所有常量放在一大行代码中之外,我不知道更好的方法!)。然后当他们需要为弹出窗口设置动画时,我会这样做:

- (void)animateIn
{   
    float pw = 247;
    float ph = 59;

    // set the view's initial position
    popupView.frame = CGRectMake(-pw*0.005+8, -ph*0.01-2, pw*0.01, ph*0.01);
    [self addSubview:popupView];

    [UIView animateWithDuration:0.12 delay:0.0 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionLayoutSubviews animations:^(void) {
        popupView.frame = CGRectMake(-pw*0.55+8, -ph*1.1-2, pw*1.1, ph*1.1);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^(void) {
            popupView.frame = CGRectMake(-pw*0.475+8, -ph*0.95-2, pw*0.95, ph*0.95);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.075 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^(void) {
                popupView.frame = CGRectMake(-round(pw/2-8), -ph-2, pw, ph);
            } completion:nil];
        }];
    }]; 
}

看起来有点复杂的动画,但实际上这是与地图视图标注视图动画相同的动画。

唯一的问题是,它在 iOS 5 设备上完美运行,但在 iOS 4 设备上,一切都错了!标签根本不显示,按钮出现然后飞走,缩小到右上角,背景图像拉伸到错误的大小和错误的位置。

还有其他人对 iOS 4 中的动画有任何问题吗?我是否必须采取与目前不同的方式才能使其发挥作用?

【问题讨论】:

    标签: uiview ios4 ios5 objective-c-blocks uiviewanimation


    【解决方案1】:

    (1) 您是否在 UIView 子类的 layoutSubviews 实现中进行手动布局?如果没有,您不应该在选项位掩码中使用UIViewAnimationOptionLayoutSubviews。我不知道这是否是问题所在,但添加可能的错误的额外来源是没有意义的。

    (2) 如有疑问,请使用 Core Animation。您可以非常轻松地将您的动画序列描述为一个不错的 CAAnimationGroup。

    【讨论】:

    • 我目前正在处理另一个项目,但我会尽快尝试...谢谢!
    • 我已经删除了 UIViewAnimationOptionLayoutSubviews 标志,它在 iOS 5 中仍然可以正常工作,非常感谢。我认为我需要它来确保它正确地为子视图设置动画。我现在将尝试使用 Core Animation 并希望它有效 - 会报告!
    • tick 表示第 2 点 - 最终使用了 Core Animation 并对其进行了排序。谢谢!
    • 你应该在它周围使用括号或那两个动画.. (UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionLayoutSubviews)
    • @user1518182 不正确。如果 Objective-C 不理解 x|y 在这里是一个表达式并且需要首先计算,那么该行甚至不会编译。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 2016-01-21
    • 2014-12-02
    相关资源
    最近更新 更多