ios - 使用 Xamarin.ios 将布局约束常量从一个值动画化为另一个值

标签 ios xamarin.ios xamarin uiviewanimation xamarin-studio

我正在使用 Xamarin Studio 并使用 Xamarin.iOS 开发 iPad 应用。

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

我现在有什么(没有动画)

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

最佳答案

在受限 View 上调用 LayoutIfNeeded(),作为 UIView.Animateanimations 操作的一部分:

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

关于ios - 使用 Xamarin.ios 将布局约束常量从一个值动画化为另一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19451785/

相关文章:

c# - 返回页面后 ListView 项目保持选中状态

c# - 从 ScrollView ( subview )内部将 View 推送到导航 Controller

android - 使用 Xamarin 扩充绑定(bind)库时出现 ClassNotFoundException

javascript - Safari/webapp 启动 native 应用程序 iOS

ios - swift 3 : Xcode claims that a non-generic class is generic

c++ - 在 Xamarin.iOS (arm64) 中使用 OpenCV

c# - 单点触控 : Using ServiceStack caused JIT error?

ios - 在 ios 应用程序中每 12 小时检查一次数据库

ios - ios内购可以使用第三方支付网关吗?

c# - Xamarin:运行进程时阻止 sleep 模式