ios - UIKit Dynamics 的性能问题

标签 ios iphone performance uikit-dynamics uidynamicanimator

最近,当我在玩 Stuart Hall 的 UIKit Dynamics 教程 (http://stuartkhall.com/posts/flipcase-bounce-in-uikit-dynamics) 时,我发现存在性能问题。

在我向动画师添加了大约 50 个项目(弹跳球)后,应用程序变得非常慢 -- 几乎卡住。分析显示 [UIDynamicAnimator _animatorStep] 占用 96% 的 CPU。

有人知道如何提高具有大量 UIDynamicItems 的 UIKit Dynamics 应用的性能吗?

你可以下载我的代码,自己看看性能问题:

https://www.dropbox.com/s/zy7ajj6molxm9up/Flipper-UIKit-Dynamics-Poor-Performance.zip

以下是一切发生的代码:

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>

@interface ViewController () {
    CMMotionManager *_motionManager;
    NSOperationQueue *_queue;

    int _count;
}

@property(nonatomic, strong) UIDynamicAnimator *animator;
@property(nonatomic, strong) UIGravityBehavior *gravityBehavior;
@property(nonatomic, strong) UICollisionBehavior *collisionBehavior;
@property(nonatomic, strong) UIDynamicItemBehavior *bounceBehaviour;

@end

@implementation ViewController

static NSInteger const kBallSize = 25;

- (void)startMotion {
    if (_motionManager == nil) {
        _queue = [[NSOperationQueue alloc] init];
        _motionManager = [[CMMotionManager alloc] init];
        _motionManager.deviceMotionUpdateInterval = 0.1;
    }
    [_motionManager startDeviceMotionUpdatesToQueue:_queue withHandler:^(CMDeviceMotion *motion, NSError *error) {
//        angleLabel.text = [NSString stringWithFormat:@"%g", motion.attitude.pitch];
        self.gravityBehavior.angle = atan2(motion.gravity.x + motion.userAcceleration.x, motion.gravity.y + motion.userAcceleration.y); // motion.attitude.pitch + M_PI / 2.0;
    }];
}

- (void)stopMotion {
    if (_motionManager) {
        [_motionManager stopDeviceMotionUpdates];
    }

    _motionManager = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self startMotion];

    // Simple tap gesture
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
    [self.view addGestureRecognizer:tapGesture];

    // Create our animator, we retain this ourselves
    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    // Gravity
    self.gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[]];
    self.gravityBehavior.magnitude = 10;
    [self.animator addBehavior:self.gravityBehavior];

    // Collision - make a fake platform
    self.collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[]];

    UIBezierPath *aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(284, 160)
                                                         radius:160
                                                     startAngle:(CGFloat) (M_PI * 0.0)
                                                       endAngle:(CGFloat) (M_PI * 2)
                                                      clockwise:YES];

    [self.collisionBehavior addBoundaryWithIdentifier:@"bottom" forPath:aPath];
    self.collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
    [self.animator addBehavior:self.collisionBehavior];

    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = aPath.CGPath;
    layer.fillColor = [UIColor lightGrayColor].CGColor;

    [self.view.layer addSublayer:layer];

    // Bounce!
    self.bounceBehaviour = [[UIDynamicItemBehavior alloc] initWithItems:@[]];
    self.bounceBehaviour.elasticity = 0.75;
    self.bounceBehaviour.resistance = 0.1;
    self.bounceBehaviour.friction = 0.01;
    [self.animator addBehavior:self.bounceBehaviour];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

#pragma mark - Gesture Recognizer

- (void)onTap:(UITapGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateEnded) {
        // Grab the x of the touch for the center of our ball
        // Ignore the y, we'll drop from the top
        CGPoint pt = [gesture locationInView:self.view];
        [self dropBallAtX:pt];
    }
}

#pragma mark - Helpers

- (void)dropBallAtX:(CGPoint)p {
    _count++;
    self.label.text = [NSString stringWithFormat:@"Balls: %d", _count];

    // Create a ball and add it to our view
    UIView *ball = [[UIView alloc] initWithFrame:CGRectMake(p.x - (kBallSize / 2), p.y - (kBallSize / 2), kBallSize, kBallSize)];
    CGFloat hue = (arc4random() % 256 / 256.0);               //  0.0 to 1.0
    CGFloat saturation = (arc4random() % 64 / 256.0) + 0.75;  //  0.5 to 1.0, away from white
    CGFloat brightness = (arc4random() % 64 / 256.0) + 0.75;
    ball.backgroundColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1.0];
    ball.layer.cornerRadius = kBallSize / 2;
    ball.layer.masksToBounds = YES;
    [self.view addSubview:ball];

    // Add some gravity
    [self.gravityBehavior addItem:ball];

    // Add the collision
    [self.collisionBehavior addItem:ball];

    // Add the bounce
    [self.bounceBehaviour addItem:ball];
}

@end

最佳答案

这让我觉得这是一个非常棘手的问题,每个球都会影响它的邻居,而邻居又会递归地影响它们的邻居,等等。对于这个非线性复杂性问题会出现相当严重的性能下降,我并不感到惊讶,因为你添加更多的球。我发现当你接近 30-40 个球时(在 iPhone 5 上),fps 开始明显下降,就像你说的,当你接近 50 个球时,它很快接近 1-2 fps。如果你让它达到静止状态(如果你保持 CMMotionManager 在混合中是不太可能的),几秒钟后 fps 恢复,但是一旦你丢下另一个球或 CMMotionManager再次改变重力,问题的复杂性很快让fps再次屈服。

简单的修复(例如增加摩擦力、减少弹跳、引入角阻力、移动到矩形、通过添加附件行为在项目停止移动后将其固定在一个位置(无论如何使用 CMMotionManager 都没有意义)等)似乎影响不大。令我印象深刻的是,解决这个问题的唯一方法是放弃 UIKit Dynamics 而采用其他一些方法,这些方法可以根据您的特定问题做出一些简化的假设(例如,因为您正在处理角度对称的圆形对象,您可以消除旋转复杂性,您可以采用更简单的碰撞逻辑,调整上述一些变量以更快地抑制行为;等等)。我不熟悉可以为您执行此操作的任何框架,因此这可能需要大量代码。即使您这样做了,您仍然在处理非线性复杂性问题,并且性能在某个时候可能仍会下降。

关于ios - UIKit Dynamics 的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24398648/

相关文章:

c++ - 增长数组 C++ 的最有效方法

ios - 如何仅比较文本中选定的字符串与 objective-c 中的给定输入

ios - UIPageViewController 与 UISegmentedControl

ios - UIAlertController 按钮代表不会触发(在 Modally Segue 中)

performance - Netty vs Nginx : Pros and Cons based on Performance, 内存管理,内存占用

c# - Linq-to-SQL 的开销

ios - 阻止 UIView 旋转为横向

iphone - 对象可以调用主应用程序吗?

iphone - 为什么我无法执行任何 CABasicAnimation 或关键帧动画内容?

iphone - 我们如何获取本地通知的关闭事件?