rich.e
rich.e

Reputation: 3750

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

Upvotes: 3

Views: 1949

Answers (2)

hfossli
hfossli

Reputation: 22992

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

Upvotes: 2

Vladimir
Vladimir

Reputation: 170859

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

Upvotes: 6

Related Questions