cocoa touch - UIImageView autoresizingmask not working in certain cases -


i experimenting block-breaking ios app learn more ui features. currently, having issues trying make work screen rotation.

i able blocks re-arrange after screen rotation having trouble getting uiimageview paddle re-arrange.

my code split follows, vc calls initializes object of blockmodel class. object stores cgrect property (which cgrect corresponding paddle's imageview).

the vc creates imageview initialized paddle image, sets autoresinging property on image view (to have flexible external masks), sets frame based on cgrect in model object , adds imageview sub-view of main view being handled vc.

the code below.

when rotate, seeing imageview not being automatically repositioned.

if image view , cgrect creation in vc, works (code sample 2).

is expected behavior? if yes, why autoresizing not kicking in if cgrect obtained property in object?

full xcode project code here (github link)

edit looks things don't work if store imageview property. doing have quick access it. why doesn't work if imageview stored property?

code model initialized

self.mymodel = [[blockermodel alloc] initwithscreenwidth:self.view.bounds.size.width                                         andheight:self.view.bounds.size.height]; 

model initialization code

-(instancetype) initwithscreenwidth:(cgfloat)width andheight:(cgfloat)height {     self = [super init];      if (self)     {         self.screenwidth = width;         self.screenheight = height;         uiimage* paddleimage = [uiimage imagenamed:@"paddle.png"];         cgsize paddlesize = [paddleimage size];         self.paddlerect = cgrectmake((self.screenwidth-paddlesize.width)/2, (1 - paddle_bottom_offset)*self.screenheight, paddlesize.width, paddlesize.height);     }      return self; } 

code in vc imageview initialized

self.paddleview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"paddle"]]; self.paddleview.backgroundcolor = [uicolor clearcolor]; self.paddleview.opaque = no; self.paddleview.autoresizingmask = uiviewautoresizingflexibletopmargin|uiviewautoresizingflexibleleftmargin|uiviewautoresizingflexiblerightmargin|uiviewautoresizingflexiblebottommargin; nslog(@"paddle rect %@",nsstringfromcgrect(self.mymodel.paddlerect)); [self.paddleview setframe:self.mymodel.paddlerect]; [self.view addsubview:self.paddleview]; 

if instead use code in vc initialize imageview things work

    uiimage* paddleimage = [uiimage imagenamed:@"paddle.png"];     cgsize paddlesize = [paddleimage size];      cgrect paddlerect = cgrectmake((self.view.bounds.size.width-paddlesize.width)/2, (1 - paddle_bottom_offset)*self.view.bounds.size.height, paddlesize.width, paddlesize.height);     uiimageview *paddleview = [[uiimageview alloc] initwithimage:paddleimage];     paddleview.backgroundcolor = [uicolor clearcolor];     paddleview.opaque = no;     paddleview.autoresizingmask = uiviewautoresizingflexibletopmargin|uiviewautoresizingflexibleleftmargin|uiviewautoresizingflexiblerightmargin|uiviewautoresizingflexiblebottommargin;      [paddleview setframe:paddlerect];     [self.view addsubview:paddleview]; 

found issue. using model object handle "game object location" logic. e.g vc calculate x axis deltas touch events & forward them model object. also, cadisplaylink events forwarded model can update ball location based on velocity , time since last event. use updated location detect collisions. split used because model class had methods detect collisions sides, paddle/ball etc.

the issue model object rewriting cgrect of paddleview adding delta received vc origin.x of current paddlerect had stored. paddlerect did not take account automatic adjustment cgrect done auto-resizing after rotation.

the fix vc set cgrect of paddlerect (set paddleview frame) before calling method in model update game properties , detect collisions. way model takes care of logic of collusion detection , updating ball movement , velocity based on it. vc uses current paddleview location , hence automatically accounts automatic adjustment cgrect done auto-resizing after rotation.

source code in github link updated.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -