Wednesday, 15 February 2012

ios - How to animate setFrame: with auto layout in Objective-C -



ios - How to animate setFrame: with auto layout in Objective-C -

i'm working on ios (6) app, , have view, size alter (animated), , within view uilabel, want grow view, not proportionally.

i.e. need view take up, 90% of view if view size, when create bigger (to size), want take up, 80%. accommodate other subviews around uilabel become visible when super view grows.

i have tried using nslayoutcontraints this, , setting leading space needs be, setting width of uilabel grow appropriately, , have tried settings leading , trailing spaces (as width automatically grow).

oddly, have other subviews nslayoutcontraints playing with, in same animation code block (using uiview animatewithduration:...) , work. of these, spacing constraints, , not size constraints, , none involve uilabels dont know if has it...

i tried setting uilabel's translatesautoresizingmaskintoconstraints = yes, , using setframe: on label, , although sets size , position of label want it, alter not animated, looks horrible...

has else had experience or ideas help me out?

thanks

following code have been trying with, "left" leading space constraint of label , "width" width constraint of label.

the setframe method called within animate code block parent view controller.

- (void)setframe:(cgrect)frame { [super setframe:frame]; float gap = ((frame.size.height - 36) - 9 * [self.account.accountdata numberofbars]) / ([self.account.accountdata numberofbars] + 1); (nslayoutconstraint *constraint in constraints) { constraint.constant = gap; } self.left.constant = frame.size.width * 34/160 + (2 - 34 * 140 / 160); self.width.constant = frame.size.width * 0.575 + 55.5; [self layoutifneeded]; }

you should able in code using constraintwithitem:attribute:relatedby:toitem:attribute:multiplier:constant:. using multiplier , constant should work. set width of label equal width of superview, multiplier of 0.7 or 0.8 , constant gives right width @ smaller size of view. view grows, constant value create smaller portion of overall width. instance, multiplier of .7 , constant of 20, if superview 100 points wide, label 90 points (100*.7 +20). if view grows 200, label 160 wide (or 80% of superview).

after edit:

actually, think method mention above won't work. in wwdc 2012 talk, "best practices mastering auto layout", talk using core animation or animating constraints directly. think case have latter. if utilize core animation, label jumps new size while view animates, if both within animation block (at to the lowest degree did me -- rob, if know better, please post). animate constraints straight (in ios) have utilize timer, , increment constraint constant. utilize nstimer, found bit jerky because of limitations of resolution (50-100 msec). smoother way utilize cadisplaylink tied display refresh rate (i think that's 16.7 msec). here's code used. setup in ib view that's 190 wide 170 wide label inside. have width constraints on both (you need add together quartzcore framework this).

#import "viewcontroller.h" #import <quartzcore/quartzcore.h> @interface viewcontroller () @property (strong,nonatomic) cadisplaylink *displaylink; @end @implementation viewcontroller { iboutlet nslayoutconstraint *labelwidthcon; iboutlet nslayoutconstraint *viewwidthcon; } -(ibaction)startviewtimer:(id)sender { [self startdisplaylink]; } -(void)startdisplaylink { self.displaylink = [cadisplaylink displaylinkwithtarget:self selector:@selector(expandview:)]; [self.displaylink addtorunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode]; } - (void)stopdisplaylink { [self.displaylink invalidate]; self.displaylink = nil; } -(void)expandview:(cadisplaylink *) link { labelwidthcon.constant += 5; viewwidthcon.constant +=10; if (viewwidthcon.constant > 370) [self stopdisplaylink]; }

ios objective-c xcode autolayout nslayoutconstraint

No comments:

Post a Comment