Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 113 lines (93 sloc) 3.102 kB
7b36a9c @jhaynie new files merge
jhaynie authored
1 /**
2 * Appcelerator Titanium Mobile
3 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
4 * Licensed under the terms of the Apache Public License
5 * Please see the LICENSE included with this distribution for details.
6 */
7
8 #import "TiUISwitch.h"
9 #import "TiUtils.h"
7e2875b Removed TiUIWidget, added TiGradient (stub), and added sizing functio…
Blain Hamon authored
10 #import "TiViewProxy.h"
7b36a9c @jhaynie new files merge
jhaynie authored
11
12 @implementation TiUISwitch
13
14 -(void)dealloc
15 {
16 [switchView removeTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
17 RELEASE_TO_NIL(switchView);
18 [super dealloc];
19 }
20
21 -(UISwitch*)switchView
22 {
23 if (switchView==nil)
24 {
25 switchView = [[UISwitch alloc] init];
26 [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
27 [self addSubview:switchView];
28 }
29 return switchView;
30 }
31
486dd11 @jhaynie make sure certain touch-based controls always receive touch events re…
jhaynie authored
32 -(BOOL)hasTouchableListener
33 {
34 // since this guy only works with touch events, we always want them
35 // just always return YES no matter what listeners we have registered
36 return YES;
37 }
38
7b36a9c @jhaynie new files merge
jhaynie authored
39 #pragma mark View controller stuff
40
41 -(void)setEnabled_:(id)value
42 {
43 [[self switchView] setEnabled:[TiUtils boolValue:value]];
44 }
45
46 -(void)setValue_:(id)value
47 {
3b2531e @jhaynie - optimize child handle by switching to read/write lock
jhaynie authored
48 // need to check if we're in a reproxy when this is set
49 // so we don't artifically trigger a change event or
50 // animate the change -- this happens on the tableview
51 // reproxy as we scroll
52 BOOL reproxying = [self.proxy inReproxy];
7b36a9c @jhaynie new files merge
jhaynie authored
53 BOOL newValue = [TiUtils boolValue:value];
3b2531e @jhaynie - optimize child handle by switching to read/write lock
jhaynie authored
54 BOOL animated = !reproxying;
7b36a9c @jhaynie new files merge
jhaynie authored
55 UISwitch * ourSwitch = [self switchView];
3705d2d @mstepanov [TIMOB-8547] iOS: switch change event gets fired when adding to a vie…
mstepanov authored
56 if ([ourSwitch isOn] == newValue) {
57 return;
58 }
3b2531e @jhaynie - optimize child handle by switching to read/write lock
jhaynie authored
59 [ourSwitch setOn:newValue animated:animated];
fa8063c @sptramer [#2295 state:fixed-in-qa] -[UISwitch isOn] does not reliably report v…
sptramer authored
60
61 // Don't rely on switchChanged: - isOn can report erroneous values immediately after the value is changed!
62 // This only seems to happen in 4.2+ - could be an Apple bug.
3705d2d @mstepanov [TIMOB-8547] iOS: switch change event gets fired when adding to a vie…
mstepanov authored
63 if ((reproxying == NO) && configurationSet && [self.proxy _hasListeners:@"change"])
3b2531e @jhaynie - optimize child handle by switching to read/write lock
jhaynie authored
64 {
fa8063c @sptramer [#2295 state:fixed-in-qa] -[UISwitch isOn] does not reliably report v…
sptramer authored
65 [self.proxy fireEvent:@"change" withObject:[NSDictionary dictionaryWithObject:value forKey:@"value"]];
3b2531e @jhaynie - optimize child handle by switching to read/write lock
jhaynie authored
66 }
7b36a9c @jhaynie new files merge
jhaynie authored
67 }
68
6434c3d TIMOB-5325 Bounds often change after center does.
Blain Hamon authored
69 -(void)frameSizeChanged:(CGRect)frame bounds:(CGRect)bounds
70 {
71 [super frameSizeChanged:frame bounds:bounds];
72 [self setCenter:[self center]];
73 }
74
cbb74b3 TIMOB 5325: Make sure the switch's frame is integers by modifying set…
Blain Hamon authored
75 -(void)setCenter:(CGPoint)center
76 {
77 CGSize ourSize = [self bounds].size;
78 CGPoint ourAnchor = [[self layer] anchorPoint];
79 CGFloat originx = center.x - (ourSize.width * ourAnchor.x);
80 CGFloat originy = center.y - (ourSize.height * ourAnchor.y);
81
82 center.x -= originx - floorf(originx);
83 center.y -= originy - floorf(originy);
84
85 [super setCenter:center];
86 }
87
7b36a9c @jhaynie new files merge
jhaynie authored
88 - (IBAction)switchChanged:(id)sender
89 {
d8cdf9c @jhaynie - fixed maps crash on device
jhaynie authored
90 NSNumber * newValue = [NSNumber numberWithBool:[(UISwitch *)sender isOn]];
91 [self.proxy replaceValue:newValue forKey:@"value" notification:NO];
92
93 //No need to setValue, because it's already been set.
94 if ([self.proxy _hasListeners:@"change"])
95 {
96 [self.proxy fireEvent:@"change" withObject:[NSDictionary dictionaryWithObject:newValue forKey:@"value"]];
97 }
7b36a9c @jhaynie new files merge
jhaynie authored
98 }
99
100 -(CGFloat)verifyWidth:(CGFloat)suggestedWidth
101 {
102 return [switchView sizeThatFits:CGSizeZero].width;
103 }
104
105 -(CGFloat)verifyHeight:(CGFloat)suggestedHeight
106 {
107 return [switchView sizeThatFits:CGSizeZero].height;
108 }
109
2da1c90 Moving layout to proxies, part 5000
Blain Hamon authored
110 USE_PROXY_FOR_VERIFY_AUTORESIZING
7b36a9c @jhaynie new files merge
jhaynie authored
111
112 @end
Something went wrong with that request. Please try again.