iOS Recipes - Matt Drance [12]
That’s it! We now have built-in, reusable toggle support for any application, from code or nib. If we were using a standard UIButton, we’d have to include a significant amount of management code in every view controller that wanted to do this. With all this logic factored away in our custom button, our controller code becomes much cleaner.
ToggleButton/Classes/ToggleButtonViewController.m
self.toggleButton = [PRPToggleButton buttonWithOnImage:self.buttonOnImage
offImage:self.buttonOffImage
highlightedImage:highlightedImage];
CGFloat buttonWidth = self.buttonOnImage.size.width;
CGFloat buttonHeight = self.buttonOffImage.size.height;
self.toggleButton.frame = CGRectMake(kButtonX, 100.0, buttonWidth, buttonHeight);
[self.view addSubview:toggleButton];
Responding to taps on PRPToggleButton is the same as it would be for any other button: just add a target/action in your code, or from IB, and do whatever is appropriate after checking the on property.
ToggleButton/Classes/ToggleButtonViewController.m
- (IBAction)toggleButtonTapped:(id)sender {
if ([sender isOn]) {
NSLog(@"Toggle button was activated!");
} else {
NSLog(@"Toggle button was deactivated!");
}
}
Be sure to look at the accompanying UIButton-based implementation alongside this code in -[ToggleButtonViewController viewDidLoad] and -[ToggleButtonViewController plainButtonTapped:] to see the how much effort is saved. Not only are we doing less work with PRPToggleButton, but the controller’s role is now clearer: the action code merely responds to the state change, rather than manages it.
Recipe 5 Form Rounded Views with Textured Colors
Problem
The UIView subclasses, buttons, and labels you use look a little dull, and you want to add some texture to the background—ideally with rounded edges and a border.
Solution
Figure 11. Rounded views with texture
* * *
In iOS all UIViews are layer-backed—meaning that a view or subview is built on its own hardware-based layer. This is great for performance because you can reposition, resize, and transform views, without having to redraw them. But you can also directly manipulate the properties of a view’s underlying layer for greater access to the inner workings of the view.
Each UIView, or subclass, exposes a layer property that is the read-only reference to its underlying layer, but all the properties of that layer can be modified. The CALayer properties of interest here include backgroundColor, borderWidth, borderColor, and cornerRadius. Setting any of these on the CALayer of any subview of UIView has a direct impact on the presentation of the view (see Figure 11, Rounded views with texture ).
We can’t get the desired look—textured, with rounded edges and a border—simply by setting the backgroundColor of the layer; for that we need to use the UIColor class method colorWithPatternImage:, which creates a repeating pattern from any image. We do need to pick the images carefully, though; otherwise, the joins in the repeats are too obvious. To avoid this problem, we can use a larger image, perhaps closer in size to the view we plan to use it with. This is especially important if we are using the pattern for the backgroundColor property, because we will effectively be setting a background image for the view. This is really easy to use because it’s still a UIColor, so any method or property that expects a UIColor object will gladly accept the patterned image.
After creating our set of patterned colors, we instantiate a normal UIButton object. We then modify the layer properties we need to give the desired effect, setting the cornerRadius to give a rounded rectangle with an 8-point border width and using the patterned colors for the borderColor and the backgroundColor.
Setting up target/action pairs for the TouchDown and TouchUpInside events,