简体   繁体   English

层移动的UIImageView XY坐标

[英]UIImageView XY Coordinates with Layer Movement

I move my UIImageView with layer around the screen. 我在屏幕上移动带有图层的UIImageView When at some point I'm trying to retrieve x and y coordinates of a moved imageView using imageView.frame.origin.x I receive original coordinates not the new ones after the move. 当我尝试使用imageView.frame.origin.x检索移动的imageView的x和y坐标时,我收到的原始坐标不是移动后的新坐标。 How to get X and Y correctly? 如何正确获得X和Y?


Here is my code: 这是我的代码:

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

Per the UIView documentation for the frame property: 根据UIView文档中的frame属性:

Warning: If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored. 警告:如果transform属性不是恒等变换,则此属性的值未定义,因此应忽略。

The frame property also becomes undefined if you adjust that view's layer's transform or affineTransform properties (which are actually what the UIView alters when you set its transform). 如果调整视图的图层的transformaffineTransform属性(实际上是UIView设置其变换时会更改的属性),则frame属性也会变为未定义。 In Cocoa Touch transforms are applied at the time of compositing by the compositor. 在Cocoa Touch中,转换是在合成器进行合成时应用的。 At present what you seem to get when a transform is applied is the original frame as though there were no transform, but as the docs say it's undefined so don't rely on that. 目前,应用转换时似乎会得到的是原始帧,好像没有转换一样,但是正如文档所说的那样,它是未定义的,所以不要依赖它。

If you're just moving the view around with no other transformation, I recommend you use the view's center property rather than applying a transform. 如果您只是在无其他变换的情况下移动视图,则建议您使用视图的center属性,而不要应用变换。 That will update your frame correctly. 那将正确地更新您的框架。 Based on empirical evidence, UIKit also seems smart enough that just adjusting the centre doesn't have any performance downside compared to applying a transform. 根据经验证据,UIKit似乎也足够聪明,以至于与应用变换相比,仅调整中心位置都不会降低性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM