代码之家  ›  专栏  ›  技术社区  ›  Dave Haigh

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

  •  0
  • Dave Haigh  · 技术社区  · 11 年前

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

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

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

    _myRightSpaceConstraint.Constant = 50;
    

    我想要什么

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

    _myRightSpaceConstraint.Constant = 300;
    

    _myRightSpaceConstraint.Constant=50;
    

    ,与上面类似,但没有指定起始常数是多少(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 ();
    
    2 回复  |  直到 11 年前
        1
  •  10
  •   Larry OBrien    11 年前

    呼叫 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();
            });
        }
    
        2
  •  -5
  •   Dave Haigh    11 年前

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

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