UIInterpolatingMotionEffect 随机工作 [英] UIInterpolatingMotionEffect works randomly

查看:22
本文介绍了UIInterpolatingMotionEffect 随机工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以这种方式向 UITableViewCell 中的某些视图添加了 UIInterpolatingMotionEffect:

I'm adding a UIInterpolatingMotionEffect to some views in UITableViewCells this way:

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

问题是效果只是随机出现,有些视图有效果,有些没有.在推送一个视图控制器并返回后,有些人可以工作,有些人则不能.

The problem is that the effect only appears randomly, where some views get the effect and some don't. After pushing a view controller and going back, some others work and some others don't.

有什么我遗漏的吗?每次重复使用单元格时都应该应用效果吗?

Is there something I'm missing? Should the effect be applied every time the cell is reused?

推荐答案

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

Using a UICollectionView, I was experiencing the same problem. After pushing a new controller, then going back to the UICollectionView, some of my cells' UIInterpolatingMotionEffects stopped functioning, but were still listed on the view's motionEffects property.

解决方案:我调用在 -layoutSubviews 中设置我的运动效果,并且每当配置单元格时,我调用 -setNeedsLayout 以确保调用 -layoutSubviews.

Solution: I called to setup my motion effects in -layoutSubviews, and whenever configuring the cell, I called -setNeedsLayout to ensure -layoutSubviews was called.

另外,每次我设置我的动作效果时,我都会删除之前的动作效果.这是关键.

Additionally, every time I setup my motion effects, I would remove the previous motion effects. This was key.

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

Here's the method I called in -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 上运行.

Hope it helps! Also, running on iOS 9.

这篇关于UIInterpolatingMotionEffect 随机工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆