我正在使用 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.Animate
的 animations
操作的一部分:
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/