【问题标题】:UIInterpolatingMotionEffect works randomlyUIInterpolatingMotionEffect 随机工作
【发布时间】:2013-10-11 13:43:38
【问题描述】:

我正在以这种方式在UITableViewCells 中的某些视图中添加UIInterpolatingMotionEffect

UIInterpolatingMotionEffect *horizontalEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
UIInterpolatingMotionEffect *verticalEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
horizontalEffect.minimumRelativeValue = @(-horizontal);
horizontalEffect.maximumRelativeValue = @(horizontal);
verticalEffect.minimumRelativeValue = @(-vertical);
verticalEffect.maximumRelativeValue = @(vertical);

UIMotionEffectGroup *effectsGroup = [UIMotionEffectGroup new];
effectsGroup.motionEffects = @[horizontalEffect, verticalEffect];

[view addMotionEffect:effectsGroup];

问题是效果只是随机出现,有些视图有效果,有些没有。推送视图控制器并返回后,其他一些可以工作,而另一些则不能。

我有什么遗漏吗?每次重用单元格时是否应该应用效果?

【问题讨论】:

  • 这段代码在哪里?它在你的 cellForRowAtIndexPath 中还是在哪里?
  • 或多或少,我在创建单元格时添加了运动效果。
  • 尽量不要使用 UIMotioneffectGroup 而是像这样添加两个动作效果:[view addMotionEffect:horizo​​ntalEffect];和 [查看 addMotionEffect:verticalEffect];
  • 不幸的是,这似乎没有任何区别。
  • 这里(几乎)相同。有没有解决这个问题?

标签: ios objective-c uitableview


【解决方案1】:

我遇到了同样的问题。我通过强制重绘所有单元格来修复它 - 使用 reloadSections:withRowAnimation: 可能适用于大多数人(或类似方法),尽管对我来说,我最终不得不编写自己的单元格初始化并重用代码,让我保持引用可变数组中每个创建的单元格,然后清除该数组并在我选择时从头开始构建。希望对您有所帮助。

【讨论】:

    【解决方案2】:

    更新: 不得不完全删除我之前的答案,因为我终于找到了更好的解决方案。

    一旦单元格被重绘/出列,看起来 iOS7/8 会与表格/集合视图中的视图的运动效果相混淆。您需要确保在单元格出列/设置后设置/更新您的运动效果。

    要正确执行此操作,您需要将运动效果逻辑移至 -layoutSubviews 方法。 然后只需在您的构造函数和方法中发送[self setNeedsLayout] 消息,您可以在单元格出列和更新后使用它来更新单元格内容。

    这完全解决了我的问题。

    【讨论】:

      【解决方案3】:

      使用UICollectionView,我遇到了同样的问题。在推送一个新控制器后,然后返回到UICollectionView,我的一些单元格的UIInterpolatingMotionEffects 停止运行,但仍列在视图的motionEffects 属性中。

      解决方案: 我打电话在-layoutSubviews 中设置我的运动效果,每当配置单元格时,我都会调用-setNeedsLayout 以确保调用-layoutSubviews

      此外,每次我设置我的动作效果时,我都会删除以前的动作效果。这是关键。

      这是我在-layoutSubviews中调用的方法:

      - (void)applyInterpolatingMotionEffectToView:(UIView *)view withParallaxLimit:(CGFloat)limit
      {
          NSArray *effects = view.motionEffects;
          for (UIMotionEffect *motionEffect in effects)
          {
              [view removeMotionEffect:motionEffect];
          }
      
          UIInterpolatingMotionEffect *effectX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath: @"center.x" type: UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
          effectX.minimumRelativeValue = @(-limit);
          effectX.maximumRelativeValue = @(limit);
      
          UIInterpolatingMotionEffect *effectY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath: @"center.y" type: UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
          effectY.minimumRelativeValue = @(-limit);
          effectY.maximumRelativeValue = @(limit);
      
          [view addMotionEffect: effectX];
          [view addMotionEffect: effectY];
      }
      

      希望对您有所帮助!此外,在 iOS 9 上运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-04
        • 1970-01-01
        • 1970-01-01
        • 2014-04-11
        • 2014-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多