How does the Bezier Path know about the UIColor when drawn?

#1

Hi!

I can’t seem to be able to figure this out: after we have defined the Bezier path object using the arc with center method, we define a new - different! - object of type UIColor, and instantiate it using the convenience method to represent a certain grey color. Then we send this color object the magical method “set stroke”. Next thing that happens is that we ask the bezier path object to draw itself using the “stroke” method. I can understand how we deal with two objects and send them methods. What I do not understand is how those two objects magically communicate in the background; how does the bezier path object now know about the UIColor object and its newly set property (the grey color)? There seems to be nothing besides the context (the arrangement in which those objects are used) that seems to convey to the system which object (the Bezier path) to apply the UIColor (light grey) to. We never, for example, sent a message to either object to associate the other object - e.g., we didn’t send a pointer to the Bezier path object along to the UIColor object when we sent the color object the “setStroke” method.

Could somebody please clarify how this magic happens? I looked a bit in the UIColor reference to “setStroke”. All it says is “Sets the color of subsequent stroke operations to the color that the receiver represents.” The object magically seems to realize when a subsequent stroke operation happens. But nothing clarifies how this happens. This behavior is freaking me out…

Anybody else realizes this - or am I alone on feeling completely puzzled? Am I in for more of such “magic”? Because, if so, I feel I’d need to be filled in more on what to expect.

Thanks!
Best regards,
Björn

#2

Alright - went on reading the chapter. In the “Further Reading” section towards the end, the issues I have expressed above are explained to a certain degree: the point is that the whole drawing process is handled by a CGDrawing context, which takes up all the commands of the drawing routine (irrespecting of the object nature), and then runs them one by one. I still have to say that this confuses me, and I would have wished for more clarity in the text; the use of different objects for one drawing context makes no sense to my young understanding of OO-programming. But, well…

Best,
Björn

#3

I agree this is confusing and could have done with some explanation. It’s not intuitive that you would have a situation in which you just declare a colour object and it somehow becomes the drawing colour without getting passed into drawing methods or properties.

#4

Unless working directly with the Quartz 2D layer, drawing is done by sending messages to seemingly unrelated objects such as a UIColor object and a UIBezierPath object. Those objects are aware of the current drawing context and modify it accordingly. The drawing context is like a canvas on which you can paint, and it is usually associated with a UIView object.

The following document’s are worth reading.

  • Quartz 2D Programming Guide
  • Drawing and Printing Guide for iOS
#5

Hi, all!

Thanks for Your replies! Good to know that also others think that this aspect is conceptually confusing.

ibex10, thanks for pointing me to further reading. I’ll be sure to have a look after finishing the book!

Best regards,
Björn