ios - 通过更新中心约束,使用 pangesture 移动 View 的中心

标签 ios xamarin.ios uipangesturerecognizer

我正在开发一个应用程序,用户可以用手指拖动 View 并更改其中心点。

但是,如果我使用自动布局创建拖动 View ,我会遇到问题。

目前,我正在以编程方式应用其高度、宽度和中心约束。

但是无法将中心约束更新到 View 中的特定点。

这就是我对其 SuperView 应用中心约束的方式。

    // Set center constraints
labelControl.CenterXAnchor.ConstraintEqualTo(this.CenterXAnchor).Active = true;
labelControl.CenterYAnchor.ConstraintEqualTo(this.CenterYAnchor).Active = true;


    // On Pangesture
this.AddGestureRecognizer(new UIPanGestureRecognizer((recognizer) =>
                {
                    CGPoint translation = recognizer.TranslationInView(recognizer.View.Superview);

                    CGPoint newCenter = new CGPoint(lastLocation.X + translation.X, lastLocation.Y + translation.Y);

                    //this.Center = newCenter; // This does not work as we have applied constraints.

                    CenterXConstraint.Constant = newCenter.X;
                    CenterYConstraint.Constant = newCenter.Y;
                }));

最佳答案

尝试

NSLayoutConstraint[] list = this.Constraints;
foreach(NSLayoutConstraint c in list)
{
     if(c.FirstAttribute == NSLayoutAttribute.CenterX)
     {
          XConstraint.Constant = newCenter.X;                       
     }
     if (c.FirstAttribute == NSLayoutAttribute.CenterY)
     {
           YConstraint.Constant = newCenter.Y;
     }
}

关于ios - 通过更新中心约束,使用 pangesture 移动 View 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56537689/

相关文章:

ios - 在几个应用程序中用新的 Swift 替换旧的 Objective-c 库

iphone - iPhone 应用程序的示例源代码,显示 UITableView 的编辑/删除/添加行

c++ - Monotouch : Adding . 资源,gcc 错误

ios - 在同一个 UIImageView 上处理/检测 UIPanGestureRecognisor 和 UISwipeGestureRecognisor

ios - 使用 UIPanGestureRecognizer 时如何使 super View 与 View 居中?

iOS - 如何在 UIPageViewController 中设置 UIScrollView 以在重新启用后开始滚动

ios - 为什么 viewDidLayoutSubviews 会干扰动画?

ios - 如何使用 NSPredicate 按日期差异大于 x 天进行过滤

xamarin.ios - 为 Visual Studio 2013 Express 安装 Xamarin

iphone - 如何在内部分发具有各种内容的 iphone 应用程序?