开发者

Why do CALayer's move slower than UIView's?

I have a UIView subclass that moved around 'event's when the user touches them, using the following overridden method:

// In a custom UIView...

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:self];
    UIView *eventView = [self.ringOfEvents hitTest:point withEvent:event];
  开发者_StackOverflow中文版  for (EventView *e in self.events) {
        if (e == eventView) {
            event.position = point;
        }
    }
}

Why is it that when I make EventView a CALayer instead of UIView, the movement slows down to a creep? I can post that code too, but it is so similar that I don't think it is necessary.

I would think that abstracting to a lower level would speed up event handling, but I must be missing something.

By the way, either if *eventView is a subclass of UIView or CALayer, the position property is as follows:

- (void)setPosition:(CGPoint)pos {
    self.layer.position = pos;
}

- (CGPoint)position {
    return self.layer.position;
}

Not sure why I get a huge decrease in latency when using UIView as apposed to CALayer..


Most CALayer properties are changed with animation by default, so decrease in latency is probably caused by that.

You may want to disable animations when layer position is changed. Possible solutions are discussed for example in here and here


This is due to implicit animations.

I've implemented a category method which removes implicit animation for givven keys and can be used like this

[view.layer setNullAsActionForKeys:@[@"bounds", @"position"]];

Implementation

@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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜