1, add a view in Rootview, and set constraints, left, right, top, all set with super margin is 20, height is 100.
2, write code control
1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4@property (Weak, nonatomic) Iboutlet UIView *Animationview;5 6 @property (nonatomic, assign) BOOL isforwadanimation;7 @end8 9 @implementationViewcontrollerTen One- (void) Viewdidload { A [Super Viewdidload]; - //additional setup after loading the view, typically from a nib. -Self.isforwadanimation =NO; the } - - //change the spacing between animationview constraints and top when you click View -- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event + { - if(self.isforwadanimation) + { A[Self ReplaceTopConstrainOnView:self.animationView contant: -]; at } - Else - { -[Self ReplaceTopConstrainOnView:self.animationView contant: $]; - } - //Start Animation in [self animateconstraints]; -Self.isforwadanimation =!self.isforwadanimation; to } + - //change the value of a constraint the- (void) Replacetopconstrainonview: (UIView *) View contant: (float) Contant * { $[Self.view.constraints enumerateobjectsusingblock:^ (Nslayoutconstraint *constraint, Nsuinteger idx, BOOL *stop) {Panax Notoginseng if(Constraint.firstitem = = View && Constraint.firstattribute = =nslayoutattributetop) - { theConstraint.constant =contant; + } A }]; the } + - //animating with UIView's class method $- (void) animateconstraints $ { -[UIView animatewithduration:2.0animations:^{ - [Self.view layoutifneeded]; the }]; -}
3, Summary
Gets FirstItem If the constraint is relative to the top
Get SecondItem If the constraint is relative to the bottom
AutoLayout and Animation