如何在iOS中显示移动对象的坐标?
我试图通过手势显示正在移动的对象的位置。一切正常,直到我想显示它的位置。代码如下:如何在iOS中显示移动对象的坐标?,ios,uipangesturerecognizer,Ios,Uipangesturerecognizer,我试图通过手势显示正在移动的对象的位置。一切正常,直到我想显示它的位置。代码如下: - (void)panDetected:(UIPanGestureRecognizer *)panRecognizer { CGPoint translation = [panRecognizer translationInView:self.view]; CGPoint imageViewPosition = _iconCamera.center; imageViewPosition.x
- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
{
CGPoint translation = [panRecognizer translationInView:self.view];
CGPoint imageViewPosition = _iconCamera.center;
imageViewPosition.x += translation.x;
// This line generates error
_labelCameraPosition.text = [NSString stringWithFormat: @"%.1f", _iconCamera.center.x];
self.iconCamera.center = imageViewPosition;
[panRecognizer setTranslation:CGPointZero inView:self.view];
}
当我想显示这个值时,图像在抖动但不移动。我该怎么办?此答案基于您正在使用自动布局 在ViewDidDisplay中,按如下方式设置标签和图像视图
self.label.translatesAutoresizingMaskIntoConstraints = YES;
self.imageView.translatesAutoresizingMaskIntoConstraints = YES;
因为在使用“自动布局”时正在更改帧。将TranslatesAutoResizengMaskintoConstraints设置为“是”,自动将其新帧转换为约束。尝试获取对象的表示层,并检查其帧:(CALayer*)self.iconCamera.layer.presentationLayer-这用于正在设置动画的视图的当前位置,不确定它在这种情况下是否有效。为什么要使用_iconCamera几次,而self.iconCamera一次?现在代码很乱。我只是想让它发挥作用。我以后会关心一致性。我喜欢这个答案!很好用!但是我不知道为什么:-)故事板创建的UIControl有它自己的约束,默认情况下,这个属性是NO,如果你移动视图,改变它的帧,旧的约束将试图打破改变(抖动)。设置为“是”后,新框架将自动向视图添加新约束,这将使视图平滑移动。