6

この方法で s のUIInterpolatingMotionEffectいくつかのビューにa を追加しています:UITableViewCell

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

問題は、効果がランダムにしか表示されず、効果が得られるビューと得られないビューがあることです。ビューコントローラーを押して戻った後、他のいくつかは機能し、他のいくつかは機能しません。

足りないものはありますか?セルを再利用するたびに効果を適用する必要がありますか?

4

3 に答える 3

0

を使用してUICollectionView、同じ問題が発生していました。新しいコントローラーを押してから に戻った後UICollectionView、一部のセルが機能を停止しましたが、ビューのプロパティUIInterpolatingMotionEffectにはまだリストされていました。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 で動作します。

于 2015-10-02T15:23:06.117 に答える
0

更新:最終的により良い解決策を見つけたため、以前の回答を完全に削除する必要がありました。

セルが再描画/デキューされると、iOS7/8 がテーブル/コレクション ビュー内のビューのモーション効果を混乱させているようです。セルがデキュー/セットアップされた後、モーション効果がセットアップ/更新されていることを確認する必要があります。

-layoutSubviewsこれを適切に行うには、モーション エフェクト ロジックをメソッドに移動する必要があります。次に[self setNeedsLayout]、コンストラクターとメソッド内でメッセージを送信するだけです。これを使用して、セルがキューから取り出されて更新された後にセルの内容を更新します。

これで問題は完全に解決しました。

于 2014-08-12T18:04:41.047 に答える