ios - Ensure arbitrarily rotated CGRect fills another when rotation occurs -


update: partially working implementation below.

i've asked couple questions on here , here.

the first works great determine if "image" rect sufficiently contained inside "crop" rect. second works little bit, something's off in implementation of it doesn't work.

i'm looking @ problem little differently, , change behavior:

  1. when user begins rotate image, i'll run check method (below) determine if needs fixing or not.
  2. if need fixing, rather waiting until user has finished rotating it, i'd resize image simultaneously fit bounds. there simpler (or more reliable) way implement behavior?
  3. i'm going block rotation greater 35ยบ in either direction don't have worry severe enlargements.

assumptions/constraints:

  1. i'm using autolayout
  2. point of rotation center of crop rect, may or may not center of image rect.
  3. this demonstrates working square crop, user can resize whatever, imagine it's going bite me in ass more when it's not square.

code:

- (bool)rotatedview:(uiview*)rotatedview containsviewcompletely:(uiview*)cropview {     // if method returns yes, good! if no, bad!      cgpoint croprotated[4];     cgrect rotatedbounds = rotatedview.bounds;     cgrect cropbounds = cropview.bounds;      // convert corner points of cropview coordinate system of rotatedview:     croprotated[0] = [cropview convertpoint:cropbounds.origin toview:rotatedview];     croprotated[1] = [cropview convertpoint:cgpointmake(cropbounds.origin.x + cropbounds.size.width, cropbounds.origin.y) toview:rotatedview];     croprotated[2] = [cropview convertpoint:cgpointmake(cropbounds.origin.x + cropbounds.size.width, cropbounds.origin.y + cropbounds.size.height) toview:rotatedview];     croprotated[3] = [cropview convertpoint:cgpointmake(cropbounds.origin.x, cropbounds.origin.y + cropbounds.size.height) toview:rotatedview];      // check if converted points within bounds of rotatedview:     return (cgrectcontainspoint(rotatedbounds, croprotated[0]) &&             cgrectcontainspoint(rotatedbounds, croprotated[1]) &&             cgrectcontainspoint(rotatedbounds, croprotated[2]) &&             cgrectcontainspoint(rotatedbounds, croprotated[3])); } 

taking yet different spin on this, i'm getting there. can see in .gif, calculations out of whack because rotation isn't should using calculate new size. how can implement correct geometry ensure image resizes correctly? time saving, put xcode project don't have fiddle around building own: https://github.com/r3mus/rotationcgrectfix.git

- (ibaction)gesturerecognized:(uirotationgesturerecognizer *)gesture {     cgfloat maxrotation = 40;     cgfloat rotation = gesture.rotation;     cgfloat currentrotation = atan2f(_imageview.transform.b, _imageview.transform.a);;      nslog(@"%0.4f", radians_to_degrees(rotation));     if ((currentrotation > degrees_to_radians(maxrotation) && rotation > 0) || (currentrotation < degrees_to_radians(-maxrotation) && rotation < 0)) {         return;     }      cgaffinetransform rotationtransform = cgaffinetransformrotate(self.imageview.transform, rotation);     gesture.rotation = 0.0f;      if (gesture.state == uigesturerecognizerstatechanged) {         cgfloat scale = sqrt(_imageview.transform.a * _imageview.transform.a + _imageview.transform.c * _imageview.transform.c);          if ((currentrotation > 0 && rotation > 0) || (currentrotation < 0 && rotation < 0))             scale = 1 + fabs(rotation);         else if (currentrotation == 0)             scale = 1;         else             scale = 1 - fabs(rotation);          cgaffinetransform sizetransform = cgaffinetransformmakescale(scale, scale);         cgpoint center = _imageview.center;         _imageview.transform = cgaffinetransformconcat(rotationtransform, sizetransform);         _imageview.center = center;     } } 

gif:

enter image description here


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -