Problem with UIBezierPath containsPoint when rotating - iphone

I got a UIView which contains a UIBezierPath. (this path is used to draw a shape in drawRect:).
Image of my setup (Blue is the UIView with the drawn shape, Red is the shape of the UIBezierPath (found by trial-error)).
Everything works fine!
but when i rotate the UIView the path does not rotate (but it draws correctly):
As you can see it is no longer updated and in sync with the UIView.
I use CGAffineTransformMakeRotation to rotate the UIView so i thought that i could do the same transformation on the UIBezierPath ([UIBezierPath applyTransform]), but this makes the beziepath disappear (i was not able to find it in the view). still drawing correctly…
any ideas?
best regards
Kristian

Could you try by creating a Container view (UIView) and use it as the parent view for both of your shapes (UIView & UIBezierPath).
Then Apply the Rotation/Animation on only Container View (Parent view).

Related

Blurry CALayer when using inside UIScrollView

I have a CustomView, inside which I have added my custom CALayer ([self.layer addSublayer:...]) which I subclassed to do some custom drawing in drawInContext method of my layer. I draw there simple paths.
The CustomView is added as subview to UIScrollView.
The problem is when I zoom, the content doesn't get redrawn so the path looks blurry.
How to fix that? I tired to call setNeedsDisplay on the layer and sublayer when zoom ends, but without results.
Example of blurry path and correct one after zooming:

crop image via drawing circle in imageview

i want to crop image via drawing circle using touchMoved event.
so how can i find the circle point in CGRect .
i have to just pass that cgrect to cropping function.
Pls Help me for drawing the circle on image and find the CGRect
Thanks
I'm not sure that I completely understand your question. I will make some assumptions:
I assume you want to display a circular region of an image, with the image cropped to only display its content within that circle; and
I assume you will be moving this circle around as the user moves his/her finger.
I will assume the non-cropped area is a solid color.
For this case, I would recommend creating a transparent png image file with a circle of transparency, and the solid background color everywhere else. You can put this in a drag-and-drop-able UIView subclass, and when the user moves his/her finger around, it will appear as if the image is being cropped in a circle that moves with their finger.
Here's how to make a UIView subclass work with drag-and-drop:
http://bynomial.com/blog/?p=77
If you need more sophisticated behavior, such as a non-solid-color background, then you could consider using an image mask with your view's CALayer property. This would be more work, because you'd have to modify the mask every time the user moved their finger. Another option would be to set the CALayer to have rounded corners (the cornerRadius property), set clipsToBounds=YES, and then move that around appropriately when the user moves their finger.
Reference for some CALayer properties, to get a sense of the cornerRadius approach (set the cornerRadius = half the width of the view, and it will be a circle):
http://bynomial.com/blog/?p=52

iPhone: In a custom view, how to draw something on top of everything else in the view?

So I made a UIView subclass let's called it 'MyView' that simply draws a line inside the DrawRect method.
I then drag a UIImageView in interface builder and put it inside MyView. But now when I run the program it's obscuring the line that I'm drawing. I'm wondering, is there a way for me to draw the line on top of the image that I have dragged into my View?
No, you'll have to reorganise the view hierarchy. CoreGraphics uses a painters model so views behind will always be drawn over by views infront. To solve this you could use a container view that holds your image view and has a transparent view (line drawing view) that sits over the image view.
Another option is to draw the image using core graphics calls in your drawRect method and then draw the line over it.
One way is to have whatever adds MyView to add your UIImageView on top of MyView. It's not ideal (you probably have good reasons for keeping MyView and the UIImageView in a single class) but definitely solves your problem (I recently did this, only, I added it to the keyWindow =)
I would use a CAShapeLayer that has a path representing the line, and insert that layer ahead of the built-in view layer. Then it should draw on top.

iPhone: Rotating a drawing without rotating the view within which it is drawn

Anyone who has gone through the Stanford CS193P class on iTunes will recognize this from Assignment 3 (HelloPoly). For those not familiar:
I have a custom view called polyView, an instance of PolygonView, a subclass of UIView. On this view, I use CGContextDrawLinearGradient to paint a color gradient over the entire rectangular view. Then I use CGContextDrawPath to stroke and fill a polygon within the bounds of polyView. And I have a UILabel called nameLabel in the center of the view (and polygon) that displays the name of the polygon (triangle, quadrilateral, pentagon, etc.). All of this works fine, and the code to do all this is in the -(void)drawRect method of the PolygonView class.
Where I ran into trouble is with an additional requirement to rotate the polygon within the view in response to user gestures. I used CGAffineTransformRotate() in response to touchesBegan() and touchesMoved() events within the PolygonView class, and this basically works, too. But I can only rotate the entire polyView, not just the polygon drawn on it. I'm sure I could go back and recalculate the path of the polygon and redraw/fill the path in response to each touchesMoved() event, but that would be expensive and can't be the best method. How can I use CGAffineTransformRotate to rotate just the polygon, without rotating the gradient-filled view or the label in the center?
Or is there some way to create the polygon on a layer that I can place over the background polyView at the desired rotation angle?
Thanks for any help you can give a beginner here!
Duane
You can do a CGContextSaveGState(...) just before doing the rotation transformation, then drawing the polygon and restoring the drawing state with a CGContextRestoreGState(...) afterwards, so as not to affect any other drawing in the view later.

My Quartz drawing on iPHone is clipped after rotation!

I'm drawing three rectangles, one of which falls off the end of the view, (i.e. the drawing is bigger than the current view bounds) so you don't see the right edge. This is good, but when the view is rotated, the right edge is still clipped, even though there's plenty of room to draw it. How can I get the view to redraw the full rectangle?
I've tried:
1) changing the frame and bounds rectangles to bigger
2) calling setNeedsLayout
3) calling setContentMode:UIViewContentModeRedraw;
4) calling [self.view setClipsToBounds:NO]
Trying a CGLayer is next, unless somebody suggests something else.
-Owen
Make sure your autoresizing mask on this layer is set properly in IB. If its not stretching the right way on rotation then the view will not take the new shape of the screen.