rotation - Getting proper perspective with multiple UIViews -
i want achieve proper perspective "tilt" on 2 separate side-by-side uiview squares. in image below red , green squares separate uiviews same transform applied. visually perspective incorrect (is it?), or @ least superior illusion shown yellow/blue square uiviews. yellow-blue squares subviews of rectangular parent uiview, , transform applied parent view.
here's code:
@interface pexviewcontroller () @property (strong, nonatomic) iboutlet uiview *redsquare; @property (strong, nonatomic) iboutlet uiview *greensquare; @property (strong, nonatomic) iboutlet uiview *yellowsquarebluesquare; @end @implementation pexviewcontroller #define tilt_amount 0.65 -(void)tiltview:(uiview *)slave{ catransform3d rotatex = catransform3didentity; rotatex.m34 = -1 / 500.0; rotatex = catransform3drotate(rotatex, tilt_amount * m_pi_2, 1, 0, 0); slave.layer.transform = rotatex; } - (void)viewdidload { [super viewdidload]; [self tiltview:self.greensquare]; [self tiltview:self.redsquare]; [self tiltview:self.yellowsquarebluesquare]; } @end
1) there simple way apply transform(s) separate red/green uiviews , achieve same effect "grouped" yellow , blue uiviews? prefer keep views separate, universal app , uiviews not side-by-side in (e.g.) ipad layout.
2) if #1 not possible, guessing best thing create parent view present in iphone, not present in ipad. other alternatives?
i opted solution #2. created short routine calculates bounding box based on array of uiviews, creates new parent view bounding box, adds arrayed views children. can apply transform parent view desired effect. here's code gathering , adopting children subviews.
-(uiview *)makeparentwithsubviews:(nsarray *)arrayofviews{ // creating bounding box uiview , add passed uiviews subview // "in-place". cgfloat xmax = -huge_valf; cgfloat xmin = huge_valf; cgfloat ymax = -huge_valf; cgfloat ymin = huge_valf; (uiview *myview in arrayofviews) { xmin = min(xmin, myview.frame.origin.x); xmax = max(xmax, myview.frame.origin.x + myview.frame.size.width); ymin = min(ymin, myview.frame.origin.y); ymax = max(ymax, myview.frame.origin.y + myview.frame.size.height); } cgfloat parentwidth = xmax - xmin; cgfloat parentheight = ymax - ymin; cgrect parentframe = cgrectmake(xmin, ymin, parentwidth, parentheight); uiview *parentview = [[uiview alloc] initwithframe:parentframe]; // replace each child's frame (uiview *myview in arrayofviews){ myview.frame = [[myview superview] convertrect:myview.frame toview:parentview]; [myview removefromsuperview]; [parentview addsubview:myview]; } parentview.backgroundcolor = [uicolor clearcolor]; [self.view addsubview:parentview]; return parentview; }
Comments
Post a Comment