使用Xamarin.ios将布局约束常量从一个值设置为另一个值

Dav*_*igh 0 xamarin.ios uiviewanimation ios xamarin xamarin-studio

我正在使用Xamarin工作室并使用Xamarin.iOS开发iPad应用程序.

我在c#文件中针对布局约束插座设置了一个常量值.

我现在拥有的(没有动画)

_myRightSpaceConstraint.Constant = 50;
Run Code Online (Sandbox Code Playgroud)

我想要的是

为此常量设置动画,使其从以下位置开始:

_myRightSpaceConstraint.Constant = 300;
Run Code Online (Sandbox Code Playgroud)

_myRightSpaceConstraint.Constant = 50;
Run Code Online (Sandbox Code Playgroud)

或者,类似于上面但没有指定起始常量是什么(300),而是我只需要在xib文件中设置它并将其设置为50.

这是否可以在Xamarin中执行此操作,如果有,是否有任何代码示例可以帮助我?

我试过的 - 哪个不起作用

UIView.BeginAnimations ("slideAnimation");
float _pt;
_pt = _myRightSpaceConstraint.Constant;

UIView.SetAnimationDuration (5);
UIView.SetAnimationCurve (UIViewAnimationCurve.EaseInOut);

UIView.SetAnimationDelegate (this);
UIView.SetAnimationDidStopSelector (
    new Selector ("slideAnimationFinished:"));

_myRightSpaceConstraint.Constant = 50;
UIView.CommitAnimations ();
Run Code Online (Sandbox Code Playgroud)

这实际上将常量成功设置为50但没有动画.

编辑/更新:

我设法通过以下方式实现了我想要的目标:

_myRightSpaceConstraint.Constant = 150;
_myViewWithTheConstraint.SetNeedsUpdateConstraints ();

UIView.BeginAnimations ("slideAnimation");

UIView.SetAnimationDuration (1);
UIView.SetAnimationCurve (UIViewAnimationCurve.EaseInOut);

UIView.SetAnimationDelegate (this);
UIView.SetAnimationDidStopSelector (
    new Selector ("slideAnimationFinished:"));

_myViewWithTheConstraint.LayoutIfNeeded ();

UIView.CommitAnimations ();
Run Code Online (Sandbox Code Playgroud)

以下是关键:

_myViewWithTheConstraint.LayoutIfNeeded ();
Run Code Online (Sandbox Code Playgroud)

Lar*_*ien 9

调用LayoutIfNeeded()受约束的视图,作为以下animations操作的一部分UIView.Animate:

    NSLayoutConstraint[] dynConstraints;
    UIView redView;
    UIView greenView;

    public ContentView()
    {
        BackgroundColor = UIColor.Blue;

        redView = new UIView() {
            BackgroundColor = UIColor.Red,
            TranslatesAutoresizingMaskIntoConstraints = false
        };
        AddSubview(redView);

        greenView = new UIView(){
            BackgroundColor = UIColor.Green,
            TranslatesAutoresizingMaskIntoConstraints = false
        };
        AddSubview(greenView);

        var viewsDictionary = new NSMutableDictionary();
        viewsDictionary["red"] = redView;
        viewsDictionary["green"] = greenView;

        dynConstraints= NSLayoutConstraint.FromVisualFormat("V:|-[red]-100-[green(==red)]-|", 0, new NSDictionary(), viewsDictionary);
        AddConstraints(dynConstraints);
        AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|-[red]-|", 0, new NSDictionary(), viewsDictionary));
        AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|-[green(==red)]", 0, new NSDictionary(), viewsDictionary));

        this.UserInteractionEnabled = true;
    }

    public override void TouchesBegan(NSSet touches, UIEvent evt)
    {
        foreach(var constraint in dynConstraints)
        {
            constraint.Constant = 50;
        }
        UIView.Animate(0.5, () => {
            redView.LayoutIfNeeded();
            greenView.LayoutIfNeeded();
        });
    }
Run Code Online (Sandbox Code Playgroud)