ios - How to prevent UILabel from being moved further than other objects -


i trying move label animation want between 2 other objects. thought nslayoutconstraints

    let labelleftconstraint = nslayoutconstraint(item: self.label, attribute: nslayoutattribute.left, relatedby: nslayoutrelation.greaterthanorequal, toitem: self.leftobject, attribute: nslayoutattribute.right, multiplier: 1, constant: 0)     let labelrightconstraint = nslayoutconstraint(item: self.label, attribute: nslayoutattribute.right, relatedby: nslayoutrelation.lessthanorequal, toitem: self.rightobject, attribute: nslayoutattribute.left, multiplier: 1, constant: 0)     let labelbottom = nslayoutconstraint(item: self.label, attribute: nslayoutattribute.bottom, relatedby: nslayoutrelation.equal, toitem: self.bottomobject , attribute: nslayoutattribute.top, multiplier: 1, constant: 0)     let labelcenterx = nslayoutconstraint(item: self.label, attribute: nslayoutattribute.centerx, relatedby: nslayoutrelation.equal, toitem: self.movingobject, attribute: nslayoutattribute.centerx, multiplier: 1, constant: 0)      label.settranslatesautoresizingmaskintoconstraints(false)     self.view.addsubview(label)     self.view.addconstraints([labelleftconstraint, labelrightconstraint, labelbottom, labelcenterx]) 

and trying update position with

    uiview.animatewithduration(0.5, delay: 0, options: uiviewanimationoptions.curveeaseout, animations: { () -> void in         self.movingobject.center = cgpoint(x: newx, y: newy)         self.view.updateconstraints()     } 

but when movingobject moving far crops label (which makes sense because of labelcenterx should equal movingobject). how prevent , instead move edges of leftobject , rightobject? need .lessequalorgreater relation...

i tried animate label .center-method didn't find way prevent getting out of 2 objects.



thank (i know asking dumb question learning swift)



edit: here screenshot of problem: the thing try animate

the movingobject picture of bike moves changing velocity left right , allowed go further label velocity on bar. label should between 0 , 30-label

edit 2: working code in git repository

the solution create required leading/trailing constraints >= 0 , centerx constraint lower priority, changing centerx constant whatever need.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

node.js - Using Node without global install -

php - CakePHP HttpSockets send array of paramms -