对比 NS_ENUM 和 NS_OPTIONS 最经典的可以用 button 的 style 和 controlState 来示例。
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // 左移动0位 2 的零次方 // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1, // 左移动1位 2 的1次方
UIControlStateSelected = 1 << 2, // 左移动2位 2 的2次方 // flag usable by app (see below)
UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // Applicable only when the screen supports focus
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, // no button type
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // standard system button
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
UIButtonTypeRoundedRect = UIButtonTypeSystem, // Deprecated, use UIButtonTypeSystem instead
};
使用 NS_ENUM 定义的通用枚举一般只是使用其中的 一个值,比如button 的样式你不可能同时使用两种样式。
使用 NS_OPTIONS 这种位移类型枚举,可以组合使用。 例如:
// 设置button 处于选中状态且高亮状态下的文字颜色
[self setTitleColor:[[AHSkinManager sharedManager] colorWithColorKey:AHThemeColorCA1] forState:UIControlStateSelected|UIControlStateHighlighted];
// 设置处于普通状态且高亮状态的文字颜色
[self setTitleColor:[[AHSkinManager sharedManager] colorWithColorKey:AHThemeColorCB2] forState:UIControlStateNormal|UIControlStateHighlighted];
注:NS_ENUM 定义的这种枚举也可以在一起使用,但是这种情况很少,我只遇到过一种(其他其实是没试过。。。) 就是在设置talbleViewCell 的编译样式的时候当 Insert和Delete同时用时是 圆圈的样式!!!!!。
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row < 2) { // 删除样式 -
return UITableViewCellEditingStyleDelete;
} else if (indexPath.row >= 2 && indexPath.row < 4) { // 插入样式 +
return UITableViewCellEditingStyleInsert;
} else if (indexPath.row >=4 && indexPath.row < 6){ // 可以选中的圆圈样式 O
return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
} else { // 无样式
return UITableViewCellEditingStyleNone;
}
}
具体可以看:UITableView的编辑样式 。