November 6, 2006
Scott Stevenson recently began a new series on Cocoa Dev Central (I really like the new layout and formatting) titled Learn Quartz. The first article, Introduction to Quartz, is necessarily simple and talks about little more than NSRect and its associated convenience methods. This is important information to understand before delving further into Cocoa drawing. At the end of the article Scott provides a code snippet to draw 12 squares, each a different color, in one function call. For me the article was a refresh for information I was already comfortable with but it did plant a seed of thought. What could make for a more interesting demonstration and yet still rely on one function call for drawing?
The first thing the demo would need is some animation. A static rectangle in a view isn’t very exciting. Animation is just the illusion of movement caused by redrawing visual objects in different positions. The most simple animation of NSRect would be to change the origin and/or size a number of times per second and cause the view to redraw. I wanted something more interesting. I wanted rotation. It doesn’t take long looking at NSRect to realize it is not possible to represent a rotated rectangular region with it. Rotation is definitely beyond the scope of what NSRect exists for but there is a way for NSRect to be rotated when drawn. Enter NSAffineTransform.
NSAffineTransform enables us to transform points in one coordinate system (such as the cartesian coordinate system utilized by Cocoa) to points in another coordinate system while preserving parallel lines in a path. One of the ways points can be transformed is by rotation. As we are working with NSRect it will be necessary to apply the transformation to the current graphics context ( NSGraphicsContext ) before we issue any drawing command. The ADC Transform Basics article provides visual representations of the effect of applying transforms to a current graphics context. In order to achieve the appearance of a rectangle spinning we would need to change the angle of rotation and redraw the view several times every second.
It is important to note that a rotation transform is rotating around the origin. If the NSRect had an origin of 0, 0 it would stay anchored at the point regardless of the rotation angle. As the rotation angle changed one corner would stay fixed at 0, 0 while the rest of the rectangle spun about that axis. This isn’t really the effect I am interested in so I set the coordinates of the NSRect such that 0, 0 is the center of my rectangle (a square in this case at origin -25, -25 and size 50, 50).
Now with the animation handled I wanted to add interactivity. I decided that the user would be able to drag the rotating rectangle around in the view space. To provide such support I would need to know when the user clicked within the rectangle. But as you will see in the code, the NSRect is never changed. So I need to determine when the user clicks within the drawn rectangle. Luckily the NSAffineTransform can be employed to transform from the drawn coordinate system to the base coordinate system.
Finally instead of having a single rectangle rotating and being dragged I have 10 of them. You can download the RectTransformDemo project.
Leave a Comment » |
Cocoa, Mac Development |
Permalink
Posted by Stephan
September 23, 2006
Tuesday I hit a major milestone for the new application I’ve been working on feverishly for the past few months. It was such a great feeling to see all the effort come together and “just work” like any true Mac application should. Suddenly it was real instead of just a collection of classes and NIB files and just as suddenly the internal development name wasn’t enough anymore. A good name is an important aspect of a successful application but it doesn’t necessarily have to convey the exact purpose of the application. What it needs to do is stand apart from other product names and be easy for people to associate with the functionality you are providing. I don’t find the process of discovering such a name to be easy but I am very happy with the result. So without further ado, the name of my new application is Anzan.
Is your curiosity piqued? Here are a couple more tidbits…
- Anzan is there when you need it and unobtrusive when you don’t
- Anzan will change the way you do something, especially in groups
Leave a Comment » |
Cocoa, Mac, Mac Development, Productivity |
Permalink
Posted by Stephan
August 24, 2006
Apple tell us that ADC Membership gets us more, but does it really? I began to wonder about this after reading The Decline of WWDC (originally from Gus Mueller). Michael and I are ADC Select members through Advenio and have been for a number of years. Do we get $500.00 worth of benefit from it each year?
The most obvious way to get value out of the Select membership is to take advantage of the Hardware Purchase Program. It doesn’t seem to say anywhere what the discount is but I will tell you that you’ve got to buy a pretty well configured machine to save $500.00. While we do use the coupon every year it could be argued that some of the purchases are spending money just to save. My old TiBook is still in active use and it would be possible to purchase a consumer Mac for less than a pro model even with the discount. We haven’t taken advantage of the Special Event Hardware Discounts because we haven’t exhibited anywhere.
Select members also receive shipping versions of Mac OS X in the mail, at least according to the website. I haven’t received anything since Tiger was released. Certainly on years without an OS release this isn’t overly useful. It also isn’t made clear how exactly we can use the license. I have 4 Macs in my household and so I purchase a family license when the OS is released. I guess I could use that one license on all of them, but that seems wrong. We can download Mac OS X Server for development and testing. This isn’t something we’ve taken advantage of because we haven’t been developing anything specifically for OS X Server.
The Developer Technical Support consultations are probably quite valuable if you are really stuck on something. I’ve never used one of the two we get per year and I don’t know if Michael has either. Generally I find answers to my questions in the documentation, the ADC website, Apple’s Mailing Lists, Cocoabuilder, CocoaDev, books I’ve purchased, or even a Google search. There are two of us and we get two consultations. To be fair that means one consultation each and just having one makes it seem so precious that it would be hard to decide when to use it. Is this problem really worth using the consultation or should I save it for a bigger challenge? That said, I think all ADC members, myself included, should strive to use these and share the knowledge gained.
Pre-Release Software is likely the most appealing aspect of ADC membership for me. Having access to new operating system features in time to support them in your software is important. So important that you might think Apple would want to make it available to all developers. Unfortunately there is no way to prove that someone who signs up for the free Online membership level of ADC is indeed developing software for the platform. However, as we’ve seen with the Leopard Sneak Peek from WWDC there are always people willing to break the NDA and make the OS available for piracy. This is a bit of sore spot with me. There are many people, particularly Vista fanboys, running Leopard without having attended WWDC and as a paying ADC member I still don’t have official access to it. We will get a seed eventually but that too will be quickly pirated.
Twice a month (pending availability) we have access to the Compatibility Labs in Cupertino. We live on the east coast. Cupertino is not a cheap place to reach. If there were a lab in Boston, or even New York it might be something we’d use. I could use access to an IPv6 compatible network right now actually, but for most consumer oriented (and non-game) applications is this level of testing important?
The most important aspects of ADC membership, namely access to the development tools and kits are available to the free Online members. This is important because it used to be the case that Apple tried to make money on their developer base. Whether the paid memberships are worthwhile for you or not depend greatly on how much you take advantage of the features of it. After this examination of Select I will strive to utilize more of the services we’ve paid for and hopefully get more from my membership.
Leave a Comment » |
Cocoa, Mac, Mac Development |
Permalink
Posted by Stephan