【问题标题】:UIView animation broken in iOS 8UIView 动画在 iOS 8 中损坏
【发布时间】:2014-11-15 03:01:46
【问题描述】:

我有一个自定义表格视图单元格,设计在一个单独的 xib 文件中,带有一个名为 unitInfoBtn 的按钮和一个名为 unitVisibilityCheckBox 的按钮。当表格进入编辑模式时,unitInfoBtn 会显示,unitVisibilityCheckBox 会出现。通过使用以下代码,这在 iOS 7 中运行良好:

-(void)didTransitionToState:(UITableViewCellStateMask)state
{
    [super didTransitionToState:state];
    //deal with entering and exiting edit mode
    if (state == UITableViewCellStateEditingMask) //cell entered edit mode
    {
        [UIView animateWithDuration:0.2 animations:^{
            self.unitInfoBtn.alpha = 0.0;
            self.unitInfoBtn.center = CGPointMake(unitInfoBtnOriginalXPos-50.0, self.unitInfoBtn.center.y);
            self.unitVisibilityCheckBox.alpha = 1.0;
        }];

    } else { //cell exited edit mode
        [UIView animateWithDuration:0.2 animations:^{
            self.unitInfoBtn.alpha = 1.0;
            self.unitInfoBtn.center = CGPointMake(unitInfoBtnOriginalXPos, self.unitInfoBtn.center.y);
            self.unitVisibilityCheckBox.alpha = 0.0;

        }];
    }       
}

但是,在 iOS 8 上,代码中断并且 alpha 设置不会改变。我该如何解决这个问题?

【问题讨论】:

    标签: ios objective-c uiview core-animation


    【解决方案1】:

    一个问题是这段代码总是错误的:

    if (state == UITableViewCellStateEditingMask)
    

    顾名思义,UITableViewCellStateEditingMask 是一个位掩码。那是因为state 是一个位掩码。不要将位掩码与== 进行比较。您正在寻找掩码中的一个特定位。您必须使用按位与并与零进行比较。

    if ((state & UITableViewCellStateEditingMask) != 0)
    

    这告诉您state 中的编辑位已设置。如果是,您正在编辑。

    state等于与 UITableViewCellStateEditingMask 的可能性很小。这意味着该位已设置而没有其他设置。你不能指望那个。我很惊讶这竟然奏效了。

    我并不是说这是导致您遇到困难的原因(因为我并不真正了解问题所在),但您当然不能指望您的代码能够正常工作。

    【讨论】:

      【解决方案2】:

      目前的状态是

      UITableViewCellStateDefaultMask (0), UITableViewCellStateShowingEditControlMask (1), UITableViewCellStateShowingDeleteConfirmationMask (2), 和 UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask (3)。

      这些状态对应于属性编辑和显示删除确认的值。可以这样测试:

      if (!cell.editing && !cell.showingDeleteConfirmation)

      {

      // 0 - UITableViewCellStateDefaultMask
      

      }

      else if (cell.editing && !cell.showingDeleteConfirmation)

      {

      // 1 - UITableViewCellStateShowingEditControlMask

      }

      else if (!cell.editing && cell.showingDeleteConfirmation)

      {

      // 2 - UITableViewCellStateShowingDeleteConfirmationMask

      }

      else if (cell.editing && cell.showingDeleteConfirmation)

      {

      // 3 - UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask
      

      }

      用这个代替 if (state == UITableViewCellStateDefaultMask) 可以正常工作。

      【讨论】:

        猜你喜欢
        • 2014-12-21
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 2015-03-29
        • 1970-01-01
        • 2012-10-10
        • 2014-11-18
        • 2013-09-21
        相关资源
        最近更新 更多