Search code examples
cocoa-touchuiviewuikitcgaffinetransform

drawInRect behavior with UIView rotation transform


I have a problem understanding how the parameter passed to the drawInRect method is defined when a rotation transformation is performed on a UIView.

To give an example I have a UIView which I rotated with an angle of 307 degree. In the drawInRect method I log the following: self.frame: {{103.932, 273.254}, {64.3007, 84.3374}} rect (the variable passed as parameter:{{0, 0.102913}, {18, 89}}

The problem is that according to the documentation I should not draw outside of rect, but considering what I should draw, there is no way my images will fit there.

Can anyone explain to me how I am supposed to use drawInRect in the case my UIView is rotated ?


To give more detail about my problem, here is what I do:

I have a scrollview with a contentView inside (subclassed). I add my UIViews in the content view.

The views in question are composed of a handler image (bottom left) and the main image (top right). Users are supposed to grab the view by pressing the handler but that's not the point.

The drawInRect method of the UIView contains the following:

[_image drawInRect:CGRectMake(handlerSize.width, 0, _image.size.width, _image.size.height)];
CGSize size = CGSizeMake(kHandPickerWidth/self.scrollViewScale, kHandPickerHeight/self.scrollViewScale);
[_handPickerImage drawInRect:(0, _image.size.height, size.width, size.height)];

The UIViews objects are added at viewWillAppear in the content view doing the following:

first instanciate,

then addSubview:

then I set the scrollViewScale parameter,

then I set the frame parameter (according to the top right image displayed (which may vary)

then I rotate the UIView.

Starting from line three, the code is executed from the

- (void)scrollViewDidZoom:(UIScrollView *)scrollView

to make sure every variable is set properly when displayed is needed.

The line defining the size variable is to adjust the marker's size no mater the zoomScale value of the scroll view.


Solution

  • So I found a solution to my problem:

    I was setting the frame parameter multiple times with some CGAffineTransformation defined which is not supposed to be done.

    Now each time I need to reset the frame I reset the affine transform, change the frame and set the back the affine transform.

    Everything works as supposed to this way.