【问题标题】:UIImageView XY Coordinates with Layer MovementUIImageView XY 坐标与图层移动
【发布时间】:2011-08-30 22:11:15
【问题描述】:

我在屏幕周围移动我的UIImageView。当我尝试使用imageView.frame.origin.x 检索移动后的 imageView 的 x 和 y 坐标时,我收到的是原始坐标,而不是移动后的新坐标。如何正确获取 X 和 Y?


这是我的代码:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect];  
imageView.animationImages = [NSArray arrayWithArray:imgNameArr];    
imageView.animationDuration = 2;
    imageView.animationRepeatCount = 0;
    [imageView startAnimating];
    [self.view addSubview:imageView];   
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = speed;
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO; 
CGMutablePathRef pointPath = CGPathCreateMutable();
CGPathMoveToPoint(pointPath, NULL, rect.origin.x, rect.origin.y);
    CGPathAddLineToPoint(pointPath, NULL, x, y);
pathAnimation.path = pointPath;
CGPathRelease(pointPath);   
[imageView.layer addAnimation:pathAnimation forKey:[NSString stringWithFormat:@"pathAnimation%@",objId]];
[imageView release];

【问题讨论】:

  • 你能包含一个代码sn-p吗?你是在调用 setNeedsLayout 还是 layoutIfNeeded?
  • 信息太少。发布一些代码怎么样?
  • 我已经发布的答案对于此代码是正确的。在 UIImageView 的中心而不是图层的位置上设置动画。否则 frame 未定义。

标签: iphone objective-c coordinates layer


【解决方案1】:

根据UIView documentationframe 属性:

警告:如果变换属性不是恒等变换,则 此属性的值未定义,因此应忽略。

如果您调整视图层的transformaffineTransform 属性,frame 属性也会变为未定义(这实际上是您设置变换时 UIView 更改的内容)。在 Cocoa Touch 中,合成器在合成时应用变换。目前,当应用转换时,您似乎得到的是原始帧,就好像没有转换一样,但正如文档所说,它是未定义的,所以不要依赖它。

如果您只是在没有其他转换的情况下移动视图,我建议您使用视图的center 属性而不是应用转换。这将正确更新您的框架。根据经验证据,UIKit 似乎也足够聪明,与应用变换相比,仅调整中心不会有任何性能下降。

【讨论】:

  • 我必须使用层,因为我的路径非常复杂。
  • 那你试过回读layer.position 属性吗?动画之后应该是正确的。甚至完全放弃 frame 属性可能是值得的——最初设置您的视图,使其具有正确的大小并位于 (0, 0),然后通过 layer.position 完成其余的工作。
  • 是的,我最近都试过了——layer.frame、layer.bounds 和 layer.position——一切都指向原始位置
  • 检查 layer.transform 会得到什么?
  • layer.transform.m11,例如是1.0;那么所有其他的大多是 0.0 和几个 1.0
猜你喜欢
  • 2016-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多