为什么CALayer的移动速度比UIView慢?

发布于 2024-12-09 11:35:47 字数 886 浏览 0 评论 0原文

我有一个 UIView 子类,当用户触摸事件时,它会使用以下重写方法来移动它们:

// 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;
        }
    }
}

为什么当我将 EventView 设为 CALayer 而不是 UIView 时,移动速度会减慢到蠕动?我也可以发布该代码,但它非常相似,我认为没有必要。

我认为抽象到较低的级别会加快事件处理速度,但我一定错过了一些东西。

顺便说一句,如果 *eventViewUIViewCALayer 的子类,则 position 属性如下:

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

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

不知道为什么使用 UIView 而不是 CALayer 时延迟会大幅减少。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

要走干脆点 2024-12-16 11:35:47

默认情况下,大多数 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

心意如水 2024-12-16 11:35:47

这是由于隐式动画造成的。

我已经实现了一个类别方法,它删除了给定键的隐式动画,并且可以像这样

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

使用

@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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文