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