-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPRPToggleButton.m
More file actions
86 lines (71 loc) · 2.41 KB
/
Copy pathPRPToggleButton.m
File metadata and controls
86 lines (71 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/***
* Excerpted from "iOS Recipes",
* published by The Pragmatic Bookshelf.
* Copyrights apply to this code. It may not be used to create training material,
* courses, books, articles, and the like. Contact us if you are in doubt.
* We make no guarantees that this code is fit for any purpose.
* Visit http://www.pragmaticprogrammer.com/titles/cdirec for more book information.
***/
//
// PRPToggleButton.m
// ToggleButton
//
// Created by Matt Drance on 11/18/09.
// Copyright 2009 Bookhouse Software, LLC. All rights reserved.
//
#import "PRPToggleButton.h"
@interface PRPToggleButton ()
@property (nonatomic, retain) UIImage *onImage;
@property (nonatomic, retain) UIImage *offImage;
@end
@implementation PRPToggleButton
@synthesize on;
@synthesize autotoggleEnabled;
@synthesize onImage;
@synthesize offImage;
- (void)dealloc {
[onImage release], onImage = nil;
[offImage release], offImage = nil;
[super dealloc];
}
+ (id)buttonWithOnImage:(UIImage *)onImage
offImage:(UIImage *)offImage
highlightedImage:(UIImage *)highlightedImage {
PRPToggleButton *button;
button = [self buttonWithType:UIButtonTypeCustom];
button.onImage = onImage;
button.offImage = offImage;
[button setBackgroundImage:offImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightedImage
forState:UIControlStateHighlighted];
button.autotoggleEnabled = YES;
return button;
}
// Set up default auto toggle if loaded from IB
- (void)awakeFromNib {
self.autotoggleEnabled = YES;
self.onImage = [self backgroundImageForState:UIControlStateSelected];
self.offImage = [self backgroundImageForState:UIControlStateNormal];
[self setBackgroundImage:nil forState:UIControlStateSelected];
}
#pragma mark Toggle support
// Change the selected state, UIButton will update appearance automatically
- (BOOL)toggle {
self.on = !self.on;
return self.on;
}
- (void)setOn:(BOOL)onBool {
if (on != onBool) {
on = onBool;
[self setBackgroundImage:(on ? self.onImage : self.offImage)
forState:UIControlStateNormal];
}
}
// Detect a "touchUpInside" and auto-toggle if so configured
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
[super endTrackingWithTouch:touch withEvent:event];
if (self.touchInside && self.autotoggleEnabled) {
[self toggle];
}
}
@end