1

drawRect passes in a rectangle which represents the area of your view to re-draw. The Apple Docs say:

Your implementation of this method should redraw the specified area of the view as quickly as possible

What is not clear to me is how you should actually proceed to use this rectangle that is passed in. Suppose your view is made up of a single Bezier Curve and drawRect asks you draw only part of that curve. Well, even this simple example is non-trivial since you must break up the curve to find the part that lies only within the rectangle.

In nearly all the example code I see for drawRect the actual argument to the method is entirely ignored. So perhaps the typical case is to just ignore this argument? What happens if you draw based on the entire view's bounds in mind, but you instead only receives a small portion of that view to this method?

johnbakers
  • 24,158
  • 24
  • 130
  • 258
  • Apple DOCS: `A UIBezierPath object combines the geometry of a path with attributes that describe the path during rendering` so it need a rectangle where it may render. So you don't need to worry about it. It will render in given UIView which is in some rect. Even though you can create object of Bezier path with some rect other wise it will create rect as per given path. You can access that part with bounds property of Bezier path – Prashant Tukadiya Dec 06 '17 at 15:29
  • @PrashantTukadiya what if the rectangle passed to `drawRect` is much smaller than the bezier path and is only for a portion of the view that a part of the path passes through, yet your drawing instructions are for the full view? – johnbakers Dec 06 '17 at 15:32
  • Suppose you have view of 100 by 100 and drawing 500 by 500 bezier path then it should show only part that been available rendered that is 100 by 100 – Prashant Tukadiya Dec 06 '17 at 15:34
  • @PrashantTukadiya suppose your view is 100 X 100 but drawRect is given a rectangle that is only 20 X 20. The drawRect's code is for the full view 100 X 100. What is drawn if the drawRect is asked for the 20 X 20? – johnbakers Dec 06 '17 at 15:35
  • Draw Rect method of view will always provide the view's actual size first time so i don't think so you may have 20 X 20 rect in drawrect method where you have 100 X 100 View. See this https://stackoverflow.com/a/32282023/4601900 – Prashant Tukadiya Dec 06 '17 at 15:37
  • You can't update some part of UIView with draw rect. Each UIView is treated as a single element. When you request a redraw, in part or whole, by calling -setNeedsDisplayInRect: or -setNeedsDisplay:, the entire view will be marked for updates. – Prashant Tukadiya Dec 06 '17 at 15:42
  • @PrashantTukadiya *Draw Rect method of view will always provide the view's actual size* ... Interesting, the docs are a bit confusing and appear to suggest that the rect could vary in size. You can also pass a custom rect to `setNeedsDisplay` and the docs suggest that it is possible to only re-render part of a view, not the whole view. – johnbakers Dec 06 '17 at 15:42
  • 1
    Yes . you can call it with different rect but here mentioned is something different Please read here . **Important** https://developer.apple.com/library/content/qa/qa1708/_index.html – Prashant Tukadiya Dec 06 '17 at 15:44
  • Also see this https://stackoverflow.com/a/16853345/4601900 – Prashant Tukadiya Dec 06 '17 at 15:47

0 Answers0