【问题标题】:CALayer Positioning After Object Has Moved物体移动后的CALayer定位
【发布时间】:2011-11-12 06:51:46
【问题描述】:

我使用 CAAnimation 使用图层属性为我的 UIImageView 设置动画,如下所示:

    [imageView.layer addAnimation:pathAnimation forKey:[NSString stringWithFormat:@"pathAnimation%@",objId]];

但是在我的动画结束时(在对象从原点移动之后),当我读取框架的 X Y 坐标或中心坐标时,它们总是看起来是原始坐标,而不是对象移动到的位置。

如何读取图层的坐标以确定移动对象的正确位置?

这是我的代码:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];  
imageView.image = [UIImage imageNamed:@"banner1.jpg"];
imageView.animationDuration = 2;
imageView.animationRepeatCount = 0;
imageView.tag = 100000;

imageView.layer.frame = imageView.frame;

[self.view addSubview:imageView];   
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = 2.0f;
pathAnimation.delegate=self;
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO; 
CGMutablePathRef pointPath = CGPathCreateMutable();
CGPathMoveToPoint(pointPath, NULL, 100, 100);
CGPathAddLineToPoint(pointPath, NULL, 300, 200);
CGPathAddLineToPoint(pointPath, NULL, 200, 150);
pathAnimation.path = pointPath;
CGPathRelease(pointPath);   
[imageView.layer addAnimation:pathAnimation forKey:@"pathAnimation"];
[imageView release];

【问题讨论】:

    标签: iphone objective-c uiimageview calayer caanimation


    【解决方案1】:

    如果您为图层设置动画,则包含图层的视图将保持在同一位置,因此框架将保持不变。 layer 也有 frame、bound 和 position 属性,所以尝试读取 layer 的属性。

    编辑:

    我在 CAKeyframeAnimation 类参考中找到了以下解释。

    在制作动画时,它会使用使用指定插值计算模式计算的值更新渲染树中的属性值。

    为了知道什么是渲染树,我在这里找到了它。

    http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/CoreAnimationArchitecture.html#//apple_ref/doc/uid/TP40006655-SW1

    似乎渲染树中的值正在更新,并且由于渲染树不同于呈现和私有,我们无法访问它。

    以下是上述链接的最后一段。

    您可以在动画事务处理过程中查询 CALayer 实例以获取其对应的表示层。如果您打算更改当前动画并希望从当前显示的状态开始新的动画,这将非常有用。

    基于此,解决方法可以是根据您提供的路径计算层的新位置并相应地设置表示层框架属性。

    如果您发现任何更新,请写在这里.....

    【讨论】:

    • 我尝试像这样读取边界 NSLog(@"%f ​​%f",imageView.layer.bounds.origin.x, imageView.layer.frame.origin.x);但我收到 [0.00 10.00] 其中 10 是正确的但仍然是原始点 - 不是最终目的地点。好的,我现在就试试“定位”……
    • Position 属性也不起作用。我使用了 imageView.layer.position.x 但结果很奇怪:对于我在 (10,10,50,50) 位置的原始视图始终变为 35,而对象移动到 100、200 等。我做错了什么吗?
    • 位置指向图层的中心。所以看起来 35 又是原来的位置。
    • 如果有帮助,请勾选此项。 stackoverflow.com/questions/630265/…
    • 有用的参考,但我需要使用复杂的路径来移动具有多个点的对象。据我了解,UIView 动画不这样做。
    【解决方案2】:

    这是自我解释的声明 (CALayer.h):

    返回包含所有属性的图层副本 在当前事务开始时,带有任何活动动画 应用。这给出了层版本的近似值 当前显示的。如果层还没有,则返回 nil 已提交。

    尝试以任何方式修改返回层的效果是 未定义。

    返回的sublayersmasksuperlayer属性 layer 返回这些属性的表示版本。这 一直到只读层方法。例如,拨打-hitTest: 在-presentationLayer 的结果上将查询演示文稿 层树的值。

    - (id)presentationLayer;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多