ios - iOS 中的断断续续的动画

标签 ios swift animation core-animation

我在 iOS 中有一个动画,代码如下:

view.layoutIfNeeded()

UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseInOut, animations: {

    // constraints which are unrelated to drawingResultsController
    self.leadingSpaceToContainerViewConstraint.constant = 0
    self.trailingSpaceToContainerViewConstraint.constant = 0

    // This controller doesn't have any constraints
    drawingResultsController.view.frame.origin = CGPoint(x: self.view.bounds.width, y: 0)
    self.view.layoutIfNeeded()

}, completion: { finished in

    drawingResultsController.view.removeFromSuperview()
    drawingResultsController.removeFromParentViewController()
    drawingResultsController.didMoveToParentViewController(nil)
})

但是,动画看起来不是很流畅。这无论如何都不可怕,但你可以肯定地说它低于 30 fps。有人知道为什么会这样吗?我已经验证了一切都在主线程上。

额外的细节:

父 Controller 的 View 有一个 subview ,它被固定到它的父 View (主视图)的边缘,这些是上面看到的约束。此 subview 包含 Controller 的主要 View (即,当 drawingResultsController 滑入和滑出屏幕时,它会产生视差效果)。 drawingResultsController 是作为主视图的 subview 添加的 subview Controller ,它没有任何约束。它滑入和滑出屏幕,这是将它滑出的代码。

这样,由约束移动的 View 是 drawingResultsController View 的兄弟。两者都是主视图的直接 subview 。

编辑:

将动画缩减为这个

UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseInOut,    animations: {

    drawingResultsController.view.frame.origin = CGPoint(x: self.view.bounds.width, y: 0)

}, completion: nil)

稍微提高了帧速率,但它仍然是一个问题。设置较长的动画时间时会更加明显。 CPU 和内存的使用在动画播放时看起来完全正常。

谢谢

最佳答案

我有过类似的问题,结果是通过重启设备解决了。

但是,这个问题似乎也可以通过让 CADisplayLink 更新属于窗口层次结构一部分的随机层来解决。我注意到了这一点,因为我们在应用程序的某个地方运行了一个由 CADisplayLink 触发的动画,这解决了应用程序其他地方的所有缓慢动画问题!

下面的 hack 解决了这个问题。将代码添加到您的主窗口类(假设您为主窗口使用自定义 UIWindow 子类):

//Hack for slow animation problem that occurs after long uptime of the device, updating a view's layer position that's in the view hierarchy using a display link solves the slow animations.

@property (nonatomic, strong) UIView *dummyView;
@property (nonatomic, strong) CADisplayLink *displayLink;

- (void)enableSlowAnimationHack {
    if (self.dummyView == nil) {
        self.dummyView = [[UIView alloc] initWithFrame:CGRectZero];
        [self addSubview:self.dummyView];
    }
    if (self.displayLink == nil) {
        self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(animateWithLink:)];
        [self.displayLink addToRunLoop: [NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    }
}

- (void)animateWithLink:(CADisplayLink *)sender {
    self.dummyView.layer.position = CGPointMake((arc4random() % 100) / 100.0, (arc4random() % 100) / 100.0);
}

关于ios - iOS 中的断断续续的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36214576/

相关文章:

JavaFX - 我是否误解了如何使用 KeyValues?

ios - Swift - 使用 NSUserDefaults 保存高分

ios - 如何用动画改变 UITableView 的宽度?

html - iOS 将不可见的 Unicode 字符显示为包含 “mvs” 的虚线框

iphone - 如何创建实例数组?

ios - 从 Swift 访问 Azure 表存储

iOS:如果我的仅限 iPad 的应用程序是使用 armv6 构建的,它会被 AppStore 拒绝吗?

swift - 仅访问匿名闭包参数的子集

objective-c - 此 bundle 无效 - 文件扩展名必须是 .zip

android - 禁用特定项目的 RecyclerView itemAnimator