【问题标题】:Optimising CALayer setPosition优化CALayer setPosition
【发布时间】:2013-12-18 01:35:50
【问题描述】:

我希望在一个应用程序中有大约 400 个 CALayer,并在动画手势期间重新定位(和缩放它们)。他们不需要在此操作期间重新绘制内容,只需移动和重新缩放即可。我想将动画保持在每秒 60 帧。

我的目标至少是任何可以运行 iOS 7 的设备。支持 iOS 6 设备也很好。

为了检查可行性,我基于 Apple 的 Single View 模板项目编写了一个小型测试应用程序。代码如下。

使用仪器进行分析,绝大多数运行时间 (94%) 都在显示链接回调中。其中 54% 用于[CALayer setPosition],33% 用于CA::Transaction::commit()。我已包含显示此内容的个人资料。

大部分位置设置配置文件跟踪似乎是关于做很多我不需要框架做的事情。那么,有谁知道是否有某种方法可以关闭所有这些东西并让图层变得更愚蠢:-) 即——如果我只想自己为所有东西制作动画,基本上启用某种快速路径?

作为一个小说明,我包含了一个提到的技术here,它通过为位置键的更改设置一个空操作来提供一些加速。

如果您有其他加快速度的想法(例如使用 UIViews 而不是 CALayers),我很想听听。

谢谢!

我希望是个人资料的相关部分:

    Running Time    Self        Symbol Name
    147914.0ms  100.0%  1754.0      -[STViewController displayLinkCallback:]
    85265.0ms   57.6%   323.0        -[CALayer setPosition:]
    83842.0ms   56.6%   8301.0        CA::Layer::set_position(CA::Vec2<double> const&, bool)
    27247.0ms   18.4%   811.0          CA::Layer::begin_change(CA::Transaction*, unsigned int, objc_object*&)
    21103.0ms   14.2%   316.0           actionForKey(CALayer*, CA::Transaction*, NSString*)
    4819.0ms    3.2%    2296.0          CAAtomGetString
    188.0ms    0.1% 188.0           -[CALayer actionForKey:]
    171.0ms    0.1% 171.0           DYLD-STUB$$x_strtod
    88.0ms    0.0%  88.0            DYLD-STUB$$-[NSString(CAMLWriter) CAMLType]
    34.0ms    0.0%  34.0            CA::Transaction::get_value(unsigned int, _CAValueType, void*)
    33.0ms    0.0%  33.0            DYLD-STUB$$-[NSNumber(CAMLWriter) CAMLType]
    23316.0ms   15.7%   730.0          CA::Layer::end_change(CA::Transaction*, unsigned int, objc_object*)
    12759.0ms    8.6%   1266.0         CA::Layer::writable_state(CA::Transaction*)
    7523.0ms    5.0%    557.0          -[NSObject(NSKeyValueObserverNotification) willChangeValueForKey:]
    1145.0ms    0.7%    1145.0         OSSpinLockLock
    594.0ms    0.4% 594.0          _Unwind_SjLj_Unregister
    327.0ms    0.2% 327.0          OSSpinLockLock$shim
    310.0ms    0.2% 310.0          CAAtomGetString
    306.0ms    0.2% 306.0          __inline_isnand
    298.0ms    0.2% 246.0          CA::Transaction::unlock()
    215.0ms    0.1% 215.0          objc_msgSend
    203.0ms    0.1% 203.0          _Unwind_SjLj_Register
    168.0ms    0.1% 168.0          -[CATextLayer didChangeValueForKey:]
    149.0ms    0.1% 149.0          DYLD-STUB$$-[NSNumber(CAMLWriter) CAMLType]
    141.0ms    0.0% 141.0          CA::Layer::property_did_change(CA::Transaction*, unsigned int)
    129.0ms    0.0% 129.0          CFRetain
    123.0ms    0.0% 123.0          CA::Transaction::lock()
    112.0ms    0.0% 112.0          CA::Transaction::ensure_compat()
    100.0ms    0.0% 100.0          _NSKeyValueRetainedObservationInfoForObject
    89.0ms    0.0%  89.0           objc_msgSend$shim
    78.0ms    0.0%  78.0           DYLD-STUB$$-[NSURL(CAMLWriter) encodeWithCAMLWriter:]
    57.0ms    0.0%  57.0           DYLD-STUB$$CAMLWriterFreeAttributeList(_CAMLWriterAttribute*)
    42.0ms    0.0%  42.0           actionForKey(CALayer*, CA::Transaction*, NSString*)
    30.0ms    0.0%  30.0           DYLD-STUB$$-[CAShapeLayer setFillColor:]
    25.0ms    0.0%  25.0           DYLD-STUB$$x_strtod
    24.0ms    0.0%  24.0           DYLD-STUB$$CFNumberFormatterSetProperty$shim
    23.0ms    0.0%  23.0           _CFRetain
    8.0ms    0.0%   8.0        magazine_alloc_
    410.0ms    0.2% 410.0         DYLD-STUB$$x_strtod
    135.0ms    0.0% 135.0         CA::Layer::begin_change(CA::Transaction*, unsigned int, objc_object*&)
    133.0ms    0.0% 133.0         -[NSObject(NSKeyValueObserverNotification) willChangeValueForKey:]
    107.0ms    0.0% 107.0         CA::Transaction::unlock()
    74.0ms    0.0%  74.0          DYLD-STUB$$-[NSURL(CAMLWriter) encodeWithCAMLWriter:]
    60.0ms    0.0%  60.0          DYLD-STUB$$CAMLWriterFreeAttributeList(_CAMLWriterAttribute*)
    53.0ms    0.0%  53.0          CA::Layer::writable_state(CA::Transaction*)
    53.0ms    0.0%  53.0          DYLD-STUB$$-[NSString(CAMLWriter) CAMLType]
    27.0ms    0.0%  27.0          CA::Layer::end_change(CA::Transaction*, unsigned int, objc_object*)
    27.0ms    0.0%  27.0          CA::Transaction::ensure_compat()
    21.0ms    0.0%  21.0          DYLD-STUB$$CA::Display::DisplayLinkItem::set_user_info(void const*)
    52572.0ms   35.5%   46.0         CA::Transaction::commit()
    38450.0ms   25.9%   185.0         CA::Context::commit_transaction(CA::Transaction*)
    33772.0ms   22.8%   75.0           CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*)
    33649.0ms   22.7%   36.0            CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*)
    33522.0ms   22.6%   458.0            CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*)
    32936.0ms   22.2%   3694.0            CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*)
    28899.0ms   19.5%   298.0              CA::Context::commit_layer(CA::Layer*, unsigned int, unsigned int, void*)
    84.0ms    0.0%  84.0               CA::Render::Object::unref() const
    83.0ms    0.0%  83.0               CA::Render::encode_set_object(CA::Render::Encoder*, unsigned long, unsigned int, CA::Render::Object*, unsigned int)
    54.0ms    0.0%  54.0               DYLD-STUB$$x_strtod
    47.0ms    0.0%  47.0               CA::Render::Encoder::encode_int32(unsigned int)
    36.0ms    0.0%  36.0               CA::Render::Layer::~Layer()
    22.0ms    0.0%  21.0               CA::Layer::thread_flags_(CA::Transaction*)
    17.0ms    0.0%  17.0               CA::Layer::copy_render_layer(CA::Transaction*, unsigned int, unsigned int*)
    128.0ms    0.0% 48.0              CA::Context::commit_layer(CA::Layer*, unsigned int, unsigned int, void*)
    91.0ms    0.0%  0.0          CA::Context::commit_layer(CA::Layer*, unsigned int, unsigned int, void*)
    46.0ms    0.0%  0.0         CA::Context::commit_layer(CA::Layer*, unsigned int, unsigned int, void*)
    1.0ms    0.0%   1.0         CA::Render::Object::unref() const
    1.0ms    0.0%   1.0         CA::Render::Encoder::encode_int32(unsigned int)
    1765.0ms    1.1%    61.0           CA::Render::Encoder::send_message(unsigned int, unsigned int)
    904.0ms    0.6% 15.0           _dispatch_queue_wakeup_global_slow
    711.0ms    0.4% 3.0        CA::Render::Encoder::~Encoder()
    100.0ms    0.0% 1.0        CA::Layer::layout_and_display_if_needed(CA::Transaction*)
    97.0ms    0.0%  13.0           pthread_mutex_unlock
    87.0ms    0.0%  14.0           _dispatch_async_f_slow
    85.0ms    0.0%  41.0           _pthread_mutex_lock
    74.0ms    0.0%  2.0        CA::Transaction::foreach_root(void (*)(CA::Layer*, void*), void*)
    70.0ms    0.0%  29.0           CA::Layer::prepare_commit(CA::Transaction*)
    66.0ms    0.0%  18.0           CA::Transaction::get_value(unsigned int, _CAValueType, void*)
    56.0ms    0.0%  31.0           CA::Render::Encoder::ObjectCache::encode_invalidations(CA::Render::Encoder*)
    51.0ms    0.0%  0.0        CA::Layer::collect_animations(CA::Transaction*, double, double*)
    38.0ms    0.0%  38.0           dispatch_async_f
    38.0ms    0.0%  38.0           _dispatch_queue_push_slow
    34.0ms    0.0%  32.0           CA::Context::retain_all_contexts(bool, CA::Context**, unsigned long&)
    33.0ms    0.0%  12.0           CA::Render::Encoder::Encoder(x_heap_struct*, unsigned int, void*, unsigned int, double)
    31.0ms    0.0%  16.0           CA::Context::unref(bool)
    29.0ms    0.0%  29.0           CA::Layer::set_next_animation_time(CA::Transaction*, double, double)
    26.0ms    0.0%  20.0           CA::Transaction::unlock()
    20.0ms    0.0%  20.0           x_heap_malloc_small_
    19.0ms    0.0%  19.0           CA::DispatchGroup::enqueue(void (*)(void*), void*)
    17.0ms    0.0%  17.0           CARecordTransaction
    17.0ms    0.0%  17.0           CABackingStoreCollectAsync
    15.0ms    0.0%  15.0           CAMarkStatistic
    15.0ms    0.0%  15.0           CALayerGetLayer
    13.0ms    0.0%  13.0           x_mem_dealloc_chain
    10.0ms    0.0%  10.0           CA::Transaction::lock()
    8.0ms    0.0%   8.0        x_heap_new_with_ptr
    7.0ms    0.0%   7.0        x_heap_free
    6.0ms    0.0%   6.0        DYLD-STUB$$-[NSString(CAMLWriter) CAMLType]
    5.0ms    0.0%   5.0        CA::Transaction::foreach_command(unsigned int, void (*)(int, unsigned long, void const*, void*), void*)
    5.0ms    0.0%   4.0        CATimeWithHostTime
    4.0ms    0.0%   4.0        DYLD-STUB$$-[OS_dispatch_data finalize]
    4.0ms    0.0%   4.0        DYLD-STUB$$-[NSNumber(CAMLWriter) CAMLType]
    3.0ms    0.0%   3.0        OSSpinLockUnlock$shim
    3.0ms    0.0%   3.0        OSSpinLockLock$shim
    3.0ms    0.0%   3.0        pthread_mutex_lock
    3.0ms    0.0%   3.0        DYLD-STUB$$dlsym
    2.0ms    0.0%   2.0        CA::CG::Queue::collect(double)
    2.0ms    0.0%   0.0        CACurrentMediaTime
    2.0ms    0.0%   2.0        CA::Render::Encoder::set_object_cache(CA::Render::Encoder::ObjectCache*)
    2.0ms    0.0%   2.0        x_heap_malloc
    2.0ms    0.0%   2.0        __mtx_droplock
    1.0ms    0.0%   1.0        OSSpinLockLock
    1.0ms    0.0%   1.0        CA::Render::Encoder::encode_int64(unsigned long long)
    1.0ms    0.0%   1.0        CA::Layer::collect_layers_(CA::Layer::CollectLayersData*)
    1.0ms    0.0%   1.0        CA::Render::Coder::Coder(x_heap_struct*)
    1.0ms    0.0%   1.0        DYLD-STUB$$CAMLWriterFreeAttributeList(_CAMLWriterAttribute*)
    1.0ms    0.0%   1.0        x_hash_table_foreach
    1.0ms    0.0%   1.0        CA::Render::Coder::~Coder()
    1.0ms    0.0%   1.0        pthread_threadid_np
    13744.0ms    9.2%   2112.0        CA::Layer::free_transaction(CA::Transaction*)
    70.0ms    0.0%  26.0          x_hash_table_free
    67.0ms    0.0%  0.0       CA::Transaction::Level::free_levels(CA::Transaction::Level*)
    34.0ms    0.0%  12.0          x_hash_table_foreach
    30.0ms    0.0%  30.0          CALayerRelease
    29.0ms    0.0%  29.0          CA::Layer::State::unref(CA::Transaction*) const
    25.0ms    0.0%  20.0          x_hash_table_remove_if
    16.0ms    0.0%  16.0          x_hash_table_size
    11.0ms    0.0%  11.0          DYLD-STUB$$__39-[CAWindowServer displayWithDisplayId:]_block_invoke
    8.0ms    0.0%   8.0       DYLD-STUB$$-[NSString(CAMLWriter) CAMLType]
    7.0ms    0.0%   7.0       x_mem_dealloc_size
    4.0ms    0.0%   4.0       OSSpinLockLock
    4.0ms    0.0%   4.0       CA::Render::Encoder::ObjectCache::encode_invalidations(CA::Render::Encoder*)
    3.0ms    0.0%   3.0       DYLD-STUB$$usleep$shim
    3.0ms    0.0%   0.0       magazine_dealloc_
    2.0ms    0.0%   2.0       DYLD-STUB$$-[NSNumber(CAMLWriter) CAMLType]
    2.0ms    0.0%   2.0       x_heap_malloc
    2.0ms    0.0%   2.0       x_heap_free
    2.0ms    0.0%   2.0       CA::Render::Encoder::~Encoder()
    2.0ms    0.0%   2.0       DYLD-STUB$$mig_put_reply_port$shim
    2.0ms    0.0%   2.0       CA::release_root_if_unused(CA::Layer*, CA::Layer*, void*)
    2.0ms    0.0%   2.0       CA::Render::Encoder::send_message(unsigned int, unsigned int)
    1.0ms    0.0%   1.0       CAMarkStatistic
    1.0ms    0.0%   1.0       CA::Layer::collect_animations(CA::Transaction*, double, double*)
    1.0ms    0.0%   1.0       pthread_mutex_unlock
    1.0ms    0.0%   1.0       CA::release_root(CA::Layer*, CA::Layer*, void*)
    1.0ms    0.0%   1.0       x_heap_malloc_small_
    1.0ms    0.0%   1.0       _pthread_mutex_lock
    1.0ms    0.0%   1.0       x_mem_dealloc_chain
    4061.0ms    2.7%    4061.0       cos
    2317.0ms    1.5%    2317.0       objc_msgSend
    1155.0ms    0.7%    1155.0       sin
    144.0ms    0.0% 0.0      +[CATransaction setAnimationDuration:]
    104.0ms    0.0% 24.0         objc_object::sidetable_release(bool)
    92.0ms    0.0%  78.0         CA::Transaction::push()
    86.0ms    0.0%  17.0         objc_object::sidetable_retain()
    76.0ms    0.0%  76.0         objc_retain
    59.0ms    0.0%  59.0         -[__NSArrayM countByEnumeratingWithState:objects:count:]
    56.0ms    0.0%  56.0         CA::Layer::set_position(CA::Vec2<double> const&, bool)
    42.0ms    0.0%  42.0         objc_release
    41.0ms    0.0%  41.0         DYLD-STUB$$objc::DenseMapBase<objc::DenseMap<objc_object*, unsigned long, true, objc::DenseMapInfo<objc_object*> >, objc_object*, unsigned long, objc::DenseMapInfo<objc_object*>, true>::erase(objc_object* const&)
    19.0ms    0.0%  19.0         CA::Transaction::pop()
    17.0ms    0.0%  17.0         +[CATransaction begin]
    17.0ms    0.0%  13.0         CATimeWithHostTime
    15.0ms    0.0%  15.0         -[CADisplayLink timestamp]
    10.0ms    0.0%  10.0         +[CATransaction commit]
    4.0ms    0.0%   4.0      os_lock_trylock
    2.0ms    0.0%   2.0      CA::Transaction::ensure_compat()
    1.0ms    0.0%   1.0      x_hash_table_remove_if
    1.0ms    0.0%   1.0      DYLD-STUB$$-[NSString(CAMLWriter) CAMLType]
    1.0ms    0.0%   1.0      DYLD-STUB$$-[NSNumber(CAMLWriter) CAMLType]
    1.0ms    0.0%   1.0      x_heap_free
    1.0ms    0.0%   1.0      x_hash_table_free
    1.0ms    0.0%   1.0      objc::DenseMapBase<objc::DenseMap<objc_object*, unsigned long, true, objc::DenseMapInfo<objc_object*> >, objc_object*, unsigned long, objc::DenseMapInfo<objc_object*>, true>::find(objc_object* const&)

这是我在模板单视图应用程序项目之上添加的代码:

//  STViewController.m
//  LayerSpeedTest

#import "STViewController.h"

@implementation CALayer (Extensions)

- (void)setNullAsActionForKeys:(NSArray *)keys
{
  NSMutableDictionary *dict = [self.actions mutableCopy];

  if(dict == nil)
  {
    dict = [NSMutableDictionary dictionaryWithCapacity:[keys count]];
  }

  for(NSString *key in keys)
  {
    [dict setObject:[NSNull null] forKey:key];
  }

  self.actions = dict;
}

@end

@interface STViewController ()
@property (nonatomic, retain) NSArray *layers;
@property (nonatomic, retain) CADisplayLink *displayLink;
-(void) displayLinkCallback:(CADisplayLink*) displayLink;
@end

@implementation STViewController

- (void)viewDidLoad
{
  [super viewDidLoad];

  UIView *const view = [self view];
  [view setBackgroundColor: [UIColor blackColor]];

  CALayer *const layer = [view layer];
  NSMutableArray *layers = [NSMutableArray array];

  for(NSUInteger i=0; i<24*3*4; ++i)
  {
    CATextLayer *const textLayer = [CATextLayer layer];
    [textLayer setContentsScale: [[UIScreen mainScreen] scale]];
    [textLayer setForegroundColor: [[UIColor whiteColor] CGColor]];
    [textLayer setBackgroundColor: [[UIColor clearColor] CGColor]];
    [textLayer setBounds: CGRectMake(0,0,100,100)];
    [textLayer setString: @"100"];
    [textLayer setNeedsDisplay];
    [textLayer setPosition: CGPointMake(160,200)];
    [textLayer setNullAsActionForKeys: @[@"position"]];
    [layer addSublayer: textLayer];
    [layers addObject: textLayer];
  }

  _layers = layers;

  _displayLink = [CADisplayLink displayLinkWithTarget: self selector: @selector(displayLinkCallback:)];
  [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}

-(void) displayLinkCallback:(CADisplayLink *)displayLink
{
  const NSTimeInterval timestamp = [displayLink timestamp]*0.3;
  [CATransaction begin];
  [CATransaction setAnimationDuration:0];
  {
    NSUInteger i=0;
    for(CALayer *const layer in _layers)
    {
      const CGFloat angle = i*0.1 + timestamp;
      [layer setPosition: CGPointMake( 160 + 100 * sin(angle), 250 + 200 * cos(angle*0.728734))];
      ++i;
    }
  }
  [CATransaction commit];
}

@end

【问题讨论】:

    标签: ios cocoa-touch animation calayer


    【解决方案1】:

    我真的认为你应该使用 Sprite Kit、OpenGLES 或任何其他东西来为 400 个项目设置动画并保持 60 fps。顺便说一句,60fps 太多了——甚至 Xcode 都要求我在测试中从 60 减少到 30。

    假设您无法从 CoreAnimation 更改为更高效的工具,您真的很生气 iOS 使用“手动”动画。不要手动设置位置,而是使用 KeyPath:

    https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html

    如果您认为必须手动设置位置,您可能需要将这种“随机”运动转换为方程式。 CA 事务更“智能”地自我管理,而不是每秒进行 60 个事务。

    最后提醒一下,您需要让模型更简单。使用 CATextLayer 是一种浪费 CPU 周期的邪恶方式——特别是如果您打算使用缩放。您可能可以通过预渲染到 CGImage 来压缩一些 FPS。

    但是说真的,请考虑不要为此使用 CoreAnimation:60 FPS 意味着您只有 16 毫秒的 CPU 时间来计算下一帧、构建场景以及渲染这些内容所需的所有内容。全部只使用一个线程(CA 不是线程安全的)。使用 GL 或 SK 可以通过在 GPU 中完成大量工作来充分利用 iOS 设备的真正力量。

    【讨论】:

    • 感谢爱德华多的回答。 Sprite Kit 听起来是个不错的建议——我没有测试它,我应该测试它。实际上,使用 UIColecitionViews 和一些技巧,我已经很顺利地完成了这项工作。我无法按照您的建议使用确定性动画/路径,因为案例的变化是对用户捏缩放的连续响应(我在问题中没有提到这一点)。
    • 哦,那是一个重要的缺失部分! :) 您可以通过降低采样频率并让 CA 填补空白来使此动画更具确定性。如果您使用 100 毫秒的周期对“捏合状态”进行采样,则可以在 100 毫秒持续时间的情况下进行大量“CATransactions”,而不会明显丢失用户手指的轨迹。 CA 会更好地保持 60 FPS。但我强烈建议设定 30 FPS 的目标:它消耗更少的电池,即使在 iPhone 3GS 中也可以实现。
    • 好的——我想我明白了。我认为您是在建议我可以将捏合动画转换为两个(或更多)关键帧,然后使用捏合比例随着用户的交互沿着曲线向前和向后滑动?
    • 我还仔细研究了 SpriteKit,我认为这可能会奏效。我可能试一试,但我现在使用 UICollectionView 还差得很远 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多