我正在开发一个应用程序,用户可以用手指拖动 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/