Search code examples
iphonecocoa-touchuikitcore-animation

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


Solution

  • 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