0

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

我在我的 c# 文件中针对布局约束出口设置了一个常量值。

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

_myRightSpaceConstraint.Constant = 50;

我想要的是

为这个常数设置动画,使其来自:

_myRightSpaceConstraint.Constant = 300;

_myRightSpaceConstraint.Constant = 50;

OR,与上面类似,但没有指定起始常量是什么(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 ();

这实际上将常量成功设置为 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 ();

以下行是关键:

_myViewWithTheConstraint.LayoutIfNeeded ();
4

2 回答 2

10

调用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();
        });
    }
于 2013-10-19T02:10:43.603 回答
-5

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

_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 ();

以下行是关键:

_myViewWithTheConstraint.LayoutIfNeeded ();
于 2013-10-21T08:21:23.170 回答