第八篇 - UISwitch
初始化
UISwitch *sw = [[UISwitch alloc] init];
添加事件
[_sw addTarget:self action:@selector(swChange:) forControlEvents:UIControlEventValueChanged];
事件处理
- (void)swChange:(UISwitch *)sw{ if (sw.on) { NSLog(@"开"); }else{ NSLog(@"关"); } }
// //NS_ASSUME_NONNULL_BEGIN // //NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UISwitch : UIControl <NSCoding> // //开启状态的颜色 @property(nullable, nonatomic, strong) UIColor *onTintColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; //关闭状态的颜色 @property(null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(6_0); //开关按钮的颜色(那个实心圆点) @property(nullable, nonatomic, strong) UIColor *thumbTintColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; // // 以下两个方法,ios7后不再生效 @property(nullable, nonatomic, strong) UIImage *onImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; @property(nullable, nonatomic, strong) UIImage *offImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; // // 设置开关状态,没有动画(主要用来判断状态,设置开关状态,用下面的) @property(nonatomic,getter=isOn) BOOL on; // - (instancetype)initWithFrame:(CGRect)frame NS_DESIGNATED_INITIALIZER; // This class enforces a size appropriate for the control, and so the frame size is ignored. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER; // //设置开关状态 - (void)setOn:(BOOL)on animated:(BOOL)animated; // does not send action // @end