Hooray! Apple NDA is over on iOS 7. Which means, this is the very first iOS 7 post at NSCookbook. For the first recipe on iOS 7 we’re gonna talk about UIKit Dynamics a bit. For those of you that don’t know what it is. One word, Awesome! Dynamics is a new framework that lets you give physics inspired animation to views.
Last updated byat .
The view controllers of your application coordinate between your data and what is actually displayed on screen. In a well written MVC application the view controllers will be where much of your essential app logic lives and in turn where you will spend a lot of time. UIKit supplies many built-in view controllers and view controllers of view controllers such as UITabBarController or UINavigationController. With these you can accomplish an amazing amount of tasks, but sometimes you need or want something a bit more custom! This article will discuss a few helpful techniques for developing and working with custom view controllers.
Last time we left off in our block discussion I had gone over some of the basics of blocks. How to implement them, call them, and gave a bit of insight on why they are cool. Now, there is one really big benefit to using blocks and that is Grand Central Dispatch. There are of course many other neat things you can do with blocks and we’ll briefly cover a few of those too.
- You Have already looked at Recipe 25: Implementing Blocks Part I.
So What Is Grand Central Dispatch?
Today, I’ll use wikipedia as my crutch:
Grand Central Dispatch (GCD) is a technology developed by Apple Inc. to optimize application support for systems with multi-core processors and other symmetric multiprocessing systems.
Ok this doesn’t tell us a whole bunch, but what it does tell us is that it is a technology developed for multi-core processors. All you need to know is GCD is a set of tools that Apple has provided that allow us to perform blocks serially or concurrently on various queues (which map to either the main thread or another GCD managed thread). We also have the option of doing this either synchronously or asynchronously.
- You have strong knowledge of Objective-C and Core Foundation.
- You have basic understanding of of KVC (Key Value Coding) within the Objective-C language
Many if not all developers use collection objects everyday while developing great apps, but few know about the power of collection operators. As part of KVC (key value coding), collection operators help make many routine tasks simple by eliminating and simplifying code.
Those of you still waiting on the blocks part II article, hang in there, I’ll get to it soon enough. For now I thought I would give ya’ll something short and sweet that is also really useful.
Every now and then new stuff comes along that makes our lives easier. One of the best things to come to Objective-C is the advent of blocks. Blocks can simplify callbacks, enumeration, and they have huge benefits in terms of multi-threading. At first, blocks will blow your mind. Block syntax is weird, but once you get the hang of em’ they’re actually kinda easy.
This Recipe is inspired by a recent problem I ran into and had quite a bit of trouble with it. Basically I wanted to create a shape and fill it with a gradient. Sounds pretty easy huh? well, not quite as we’ll find out shortly. While my particular application required filling a circle with an ellipse, we can create any shape. In essence we’re creating a window (shaped however we want) where the user can see only a portion of the drawing behind it.
Working with Xcode can sometimes be a frustrating experience…
However, Xcode has a few times saving features that really make a difference in day to day development. In this post we are going to break from the traditional format in order to examine a few of these features.
Xcode snippets have gotten a bad wrap since they were introduced. This is mostly because they seem to lack some obvious features and are poorly documented. However, this doesn’t stop them from being an immensely powerful addition to Xcode. So let’s dive into using existing snippets and creating new ones.
This recipe will demonstrate how to use CAGradientLayer to add gradients to a custom UIView.
- You should have basic knowledge of UIView and Core Animation, specifically CALayer.
- Basic knowledge of autolayout usage in interface builder.
Setting Up A Sample Application
- Create a new Single View Application in Xcode named GradientViewDemo. Make sure to check Use Automatic Reference Counting and uncheck Use Storyboards. Our demo app will also be iPhone only to keep things simple, but go ahead make a universal app if you like (everything we will do is applicable on both iPhone and iPad),
- Create a new UIView subclass named NSCBGradientView. We added the NSCB class prefix, because this may be a valuable component to reuse in future projects and the prefix will help avoid name clashing.
- Next, we need to add QuartzCore to the target. Do so by selecting the project file in the source list on the left, then ensure the GradientViewDemo target has been selected, select the Build Phases tab from the tab bar at the top, expand the Link Binary with Libraries, and finally click the + button in the lower left corner. Search for QuartzCore, and then add it to the target.
- You have enough familiarity with UIKit and Xcode to create projects and UIViews. If not, consider starting with Recipe 1 and reviewing Familiarizing Yourself With Xcode
Even the slightest drop shadow here and there can dramatically improve the look of your application’s UI, but at what cost?
Everyone wants to make their app look unique! Well the UIAppearance protocol can help you! In iOS 5.0 Apple introduced the UIAppearance proxy API which allows you the developer to customize many of the appearance aspects of UIKit elements. I know what you’re thinking… Can’t I already do that? Well sure, but not on the same scale as UIAppearance will allow, and furthermore, changing the appearance of UI components on a per object basis gets really old, really fast.
So let’s dive into an example right quick…
Say you have an app with a UINavigationBar and you want something other than the default background color (tint color). You would probably try something similar to the following
Then you decide another part of your app needs a navigation bar and you end up writing the same code to change the background color for that one as well. This continues as time goes on and before long you end up with an unmanageable mess, leaving you sad and depressed wishing there was a better way… enter UIAppearance…