Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center two views using view.frame and view.center

Tags:

ios

animation

I am trying to do something pretty basic here. I am using a block-based UIView animation to bring the subview that is currently off the screen to the current view's center. Obviously the sp.view.frame = self.view.center line is the issue here. In the end, how do I do what I want?

[UIView animateWithDuration:1 delay:0
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{
    sp.view.frame = self.view.center;        
}

Thanks!

like image 621
SimplyKiwi Avatar asked Sep 12 '11 21:09

SimplyKiwi


1 Answers

It is possible to add a macro like this
#define countmid(X,Y) (float) (X-Y)/2
and then use it in code as
float Center = countmid(VIEW.frame.size.width,SUB.frame.size.width);
to place your SUB view at horizontal center of VIEW
The same for Vertical center, just use height against width

like image 77
ETech Avatar answered Oct 20 '22 00:10

ETech