objective c - View jumps back after setting center in iOS 8 -


i’m beginner xcode, , have problem counter in app.

when put timer count points player(paddle) jump in place in first time(in middle) everytime when timer count point.

so should keep paddle is? there other way count points?

its doesnt matter how control player swipe or g-sensor, in example control swipe. , problem appears in ios 8.0> , not in ios 7.

here code:

.h file:

int scorenumber;  @interface viewcontroller : uiviewcontroller{        iboutlet uilabel *scorelabel;     nstimer *timer; }  @property (nonatomic, strong)iboutlet uiimageview *paddle;     @property (nonatomic) cgpoint paddlecenterpoint;    @end 

.m file:

@implementation viewcontroller  -(void)touchesmoved:(nsset *)touches withevent:(uievent *)event{      uitouch *touch = [touches anyobject];     cgpoint touchlocation = [touch locationinview:self.view];      cgfloat ypoint = self.paddlecenterpoint.y;     cgpoint paddlecenter = cgpointmake(touchlocation.x, ypoint);      self.paddle.center = paddlecenter;         }  -(void)score{         scorenumber = scorenumber +1;     scorelabel.text = [nsstring stringwithformat:@"%i", scorenumber]; }  -(void)viewdidload {     timer = [nstimer scheduledtimerwithtimeinterval:1 target:self selector:@selector(score) userinfo:nil repeats:yes];      [super viewdidload];     // additional setup after loading view, typically nib. } 

in ios 8, autolayout king, , setting center doesn't change layout constraints. has nothing timer. has changing label's text. when happens, triggers layout of whole view, , reasserts constraints (via layoutifneeded). though you're not setting constraints in ib, xcode automatically insert them during build.

there several solutions. first, insert paddle programmatically in viewdidload, bypass constraints system. specific problem, that's ok (and might how i'd personally). this:

@interface viewcontroller () @property (nonatomic, strong) uiview *paddle; @end  @implementation viewcontroller  - (void)viewdidload {     self.paddle = [[uiview alloc] initwithframe:cgrectmake(100, 100, 200, 50)];     self.paddle.backgroundcolor = [uicolor bluecolor];     [self.view addsubview:self.paddle]; }  - (void)touchesmoved:(nsset *)touches withevent:(uievent *)event {     uitouch *touch = [touches anyobject];     cgpoint touchlocation = [touch locationinview:self.view];     self.paddle.center = cgpointmake(touchlocation.x, self.paddle.center.y); }     @end 

but how can solve without throwing away constraints? well, modify constraint. add paddle view in ib, , add constraints left, bottom, width, , height. create ib outlet horizontal , width constraints.

enter image description here

edit horizontal space constraint remove "relative margin" both first , second items:

enter image description here

now, can modify constraint rather center:

@interface viewcontroller () @property (nonatomic, weak) iboutlet uiview *paddle; @property (weak, nonatomic) iboutlet nslayoutconstraint *paddlehorizontalconstraint; @property (weak, nonatomic) iboutlet nslayoutconstraint *paddlewidthconstraint; @end  @implementation viewcontroller  - (void)touchesmoved:(nsset *)touches withevent:(uievent *)event {     uitouch *touch = [touches anyobject];     cgpoint touchlocation = [touch locationinview:self.view];      cgfloat width = self.paddlewidthconstraint.constant;      self.paddlehorizontalconstraint.constant = touchlocation.x - width/2; }     @end 

you need read width constraint because frame or bounds may not correct until viewdidlayoutsubviews: called. don't want update constraint there, since that'll force layout.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -