Procházet zdrojové kódy

Add switch argument input view subclass for switches.

Ryan Olson před 12 roky
rodič
revize
dd51356466

+ 13 - 0
Classes/Editing/Argument Input Views/FLEXArgumentInputSwitchView.h

@@ -0,0 +1,13 @@
+//
+//  FLEXArgumentInputSwitchView.h
+//  Flipboard
+//
+//  Created by Ryan Olson on 6/16/14.
+//  Copyright (c) 2014 Flipboard. All rights reserved.
+//
+
+#import "FLEXArgumentInputView.h"
+
+@interface FLEXArgumentInputSwitchView : FLEXArgumentInputView
+
+@end

+ 92 - 0
Classes/Editing/Argument Input Views/FLEXArgumentInputSwitchView.m

@@ -0,0 +1,92 @@
+//
+//  FLEXArgumentInputSwitchView.m
+//  Flipboard
+//
+//  Created by Ryan Olson on 6/16/14.
+//  Copyright (c) 2014 Flipboard. All rights reserved.
+//
+
+#import "FLEXArgumentInputSwitchView.h"
+
+@interface FLEXArgumentInputSwitchView ()
+
+@property (nonatomic, strong) UISwitch *inputSwitch;
+
+@end
+
+@implementation FLEXArgumentInputSwitchView
+
+- (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding
+{
+    self = [super initWithArgumentTypeEncoding:typeEncoding];
+    if (self) {
+        self.inputSwitch = [[UISwitch alloc] init];
+        [self.inputSwitch addTarget:self action:@selector(switchValueDidChange:) forControlEvents:UIControlEventValueChanged];
+        [self.inputSwitch sizeToFit];
+        [self addSubview:self.inputSwitch];
+    }
+    return self;
+}
+
+
+#pragma mark Input/Output
+
+- (void)setInputValue:(id)inputValue
+{
+    BOOL on = NO;
+    if ([inputValue isKindOfClass:[NSNumber class]]) {
+        NSNumber *number = (NSNumber *)inputValue;
+        on = [number boolValue];
+    } else if ([inputValue isKindOfClass:[NSValue class]]) {
+        NSValue *value = (NSValue *)inputValue;
+        if (strcmp([value objCType], @encode(BOOL)) == 0) {
+            [value getValue:&on];
+        }
+    }
+    self.inputSwitch.on = on;
+}
+
+- (id)inputValue
+{
+    BOOL isOn = [self.inputSwitch isOn];
+    NSValue *boxedBool = [NSValue value:&isOn withObjCType:@encode(BOOL)];
+    return boxedBool;
+}
+
+- (void)switchValueDidChange:(id)sender
+{
+    [self.delegate argumentInputViewValueDidChange:self];
+}
+
+
+#pragma mark - Layout and Sizing
+
+- (void)layoutSubviews
+{
+    [super layoutSubviews];
+    
+    CGFloat originY = 0;
+    if (self.showsTitle) {
+        originY = CGRectGetMaxY(self.titleLabel.frame) + [[self class] titleBottomPadding];
+    }
+    
+    self.inputSwitch.frame = CGRectMake(0, originY, self.inputSwitch.frame.size.width, self.inputSwitch.frame.size.height);
+}
+
+- (CGSize)sizeThatFits:(CGSize)size
+{
+    CGSize fitSize = [super sizeThatFits:size];
+    fitSize.height += self.inputSwitch.frame.size.height;
+    return fitSize;
+}
+
+
+#pragma mark - Class Helpers
+
++ (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value
+{
+    // Only BOOLs. Current value is irrelevant.
+    return strcmp(type, @encode(BOOL)) == 0;
+}
+
+@end

+ 4 - 1
Classes/Editing/Argument Input Views/FLEXArgumentInputViewFactory.m

@@ -10,6 +10,7 @@
 #import "FLEXArgumentInputView.h"
 #import "FLEXArgumentInputJSONObjectView.h"
 #import "FLEXArgumentInputNumberView.h"
+#import "FLEXArgumentInputSwitchView.h"
 
 @implementation FLEXArgumentInputViewFactory
 
@@ -31,7 +32,9 @@
     // Note that order is important here since multiple subclasses may support the same type.
     // An example is the number subclass and the bool subclass for the type @encode(BOOL).
     // Both work, but we'd prefer to use the bool subclass.
-    if ([FLEXArgumentInputNumberView supportsObjCType:typeEncoding withCurrentValue:currentValue]) {
+    if ([FLEXArgumentInputSwitchView supportsObjCType:typeEncoding withCurrentValue:currentValue]) {
+        argumentInputViewSubclass = [FLEXArgumentInputSwitchView class];
+    } else if ([FLEXArgumentInputNumberView supportsObjCType:typeEncoding withCurrentValue:currentValue]) {
         argumentInputViewSubclass = [FLEXArgumentInputNumberView class];
     } else if ([FLEXArgumentInputJSONObjectView supportsObjCType:typeEncoding withCurrentValue:currentValue]) {
         argumentInputViewSubclass = [FLEXArgumentInputJSONObjectView class];