GKLCubeViewController
GKLCubeViewController copied to clipboard
Constrain cube height within screen bounds as it rotates
I have another enhancement request for this (sorry!)
To make this a really polished control, it would be great if there was a way to constrain the cube vertically when it rotates so that the top and bottom corners of the cube stay within the screen bounds as it rotates. This behaviour can be found in apps like Clear and WeatherCube
I tried to implement something like this myself by dynamically changing the z-axis translation and perspective as the cube rotates:
CGFloat compensateX = (M_PI_2 - fabs(angle)) * 100.0;
double halfWidth = self.view.bounds.size.width / 2.0;
CGFloat perspective = kPerspective * (fabs(angle) / M_PI_2);
CATransform3D transform = CATransform3DIdentity;
if (applyPerspective) transform.m34 = perspective;
transform = CATransform3DTranslate(transform, 0, 0, -halfWidth - compensateX);
However, this vertically constrains the cube correctly, but results in the two views making the front-most corner of the cube "splitting apart" as the vertex approaches the centre of the screen. I guess I could try tweaking some more to get it right, but I'm not sure I'm along the right lines with this. Is there a better way?
Maybe @robertmryan has a view on this?
@jamiebullock, no reason to apologize, this is what repo issues are meant for. I definitely like your suggestion, +1 to keeping it within bounds. I'll see if I can take a look at it tonight, though @robertmryan might end up sneaking in and fixing it before me.
@jamiebullock I'll take a look at it. There's a complex subtle interplay between the existing set of three transformations that achieve the 3D cube effect, so it's non-trivial, but certainly possible. I was actually buried in the subtleties of the trigonometry underlying the transformations today, so so it won't be hard for me to tackle this. I'm trying to put some finishing touches on some other changes tonight, but will take a look tomorrow.