UIControlEvents
是否可以从 aNSString
或类似变量创建变量?例如,我可能有一个NSString
被调用的,它与变量UIControlEventTouchUpInside
的 typedef 之一相同。UIControlEvents
感谢大家的帮助!
UIControlEvents
是否可以从 aNSString
或类似变量创建变量?例如,我可能有一个NSString
被调用的,它与变量UIControlEventTouchUpInside
的 typedef 之一相同。UIControlEvents
感谢大家的帮助!
如果我理解它是如何正常工作的,UIControlEvents 枚举如下:
enum {
UIControlEventTouchDown = 1 << 0,
UIControlEventTouchDownRepeat = 1 << 1,
UIControlEventTouchDragInside = 1 << 2,
UIControlEventTouchDragOutside = 1 << 3,
UIControlEventTouchDragEnter = 1 << 4,
UIControlEventTouchDragExit = 1 << 5,
UIControlEventTouchUpInside = 1 << 6,
UIControlEventTouchUpOutside = 1 << 7,
UIControlEventTouchCancel = 1 << 8,
UIControlEventValueChanged = 1 << 12,
UIControlEventEditingDidBegin = 1 << 16,
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19,
UIControlEventAllTouchEvents = 0x00000FFF,
UIControlEventAllEditingEvents = 0x000F0000,
UIControlEventApplicationReserved = 0x0F000000,
UIControlEventSystemReserved = 0xF0000000,
UIControlEventAllEvents = 0xFFFFFFFF
};
所以再一次,这是如果我正确理解这一点,如果我错了,请纠正我,但你应该能够制作一个变量整数并将其传递给控制事件,如下所示:
int myVariable = 64;
[myButton addTarget:self action:@selector(mySelector) forControlEvents:myVariable];
有关按位左移的说明,请参阅此链接
您必须创建自己的字典才能从一个映射到另一个:
NSDictionary * const controlEventTypesFromStrings = @{
@"UIControlEventTouchDown" : @(UIControlEventTouchDown),
@"UIControlEventTouchDownRepeat" : @(UIControlEventTouchDownRepeat),
//etc.
这是仍然较新的Clang 文字语法;您当然可以使用旧/标准制作字典[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInteger:UIControlEventTouchDown], @"UIControlEventTouchDown", ...
然后用你的字符串,你会NSNumber
从字典中得到一个:
NSNumber * eventTypeNum = controlEventTypesFromStrings[stringDescribingControlEvent];
UIControlEvents eventType = [eventTypeNum unsignedIntegerValue];
如果您不使用新的下标语法,则NSNumber
可以通过检索来完成。objectForKey:
下面是一些描述 UIControlEvents 位掩码中位的快速代码:
- (NSString *)describeEvents:(UIControlEvents)event {
NSDictionary *events = @{@0: @"UIControlEventTouchDown",
@1: @"UIControlEventTouchDownRepeat",
@2: @"UIControlEventTouchDragInside",
@3: @"UIControlEventTouchDragOutside",
@4: @"UIControlEventTouchDragEnter",
@5: @"UIControlEventTouchDragExit",
@6: @"UIControlEventTouchUpInside",
@7: @"UIControlEventTouchUpOutside",
@8: @"UIControlEventTouchCancel",
@12: @"UIControlEventValueChanged",
@13: @"UIControlEventPrimaryActionTriggered",
@16: @"UIControlEventEditingDidBegin",
@17: @"UIControlEventEditingChanged",
@18: @"UIControlEventEditingDidEnd",
@19: @"UIControlEventEditingDidEndOnExit"};
NSMutableString *result = [[NSMutableString alloc] init];
for(int i = 0; i<sizeof(UIControlEvents) * CHAR_BIT; i++) {
if((event & ((NSUInteger)1 << i)) != 0) {
NSString *eventName = [events objectForKey:[NSNumber numberWithInt:i]];
if(!eventName)
eventName = [NSString stringWithFormat:@"%i", i];
if(result.length > 0)
[result appendString:@", "];
[result appendString:eventName];
}
}
return result;
}
示例输出:
[self describeEvents:UIControlEventTouchCancel] // UIControlEventTouchCancel
[self describeEvents:UIControlEventAllTouchEvents] // UIControlEventTouchDown, UIControlEventTouchDownRepeat, UIControlEventTouchDragInside, UIControlEventTouchDragOutside, UIControlEventTouchDragEnter, UIControlEventTouchDragExit, UIControlEventTouchUpInside, UIControlEventTouchUpOutside, UIControlEventTouchCancel, 9, 10, 11
[self describeEvents:UIControlEventAllEvents] // UIControlEventTouchDown, UIControlEventTouchDownRepeat, UIControlEventTouchDragInside, UIControlEventTouchDragOutside, UIControlEventTouchDragEnter, UIControlEventTouchDragExit, UIControlEventTouchUpInside, UIControlEventTouchUpOutside, UIControlEventTouchCancel, 9, 10, 11, UIControlEventValueChanged, UIControlEventPrimaryActionTriggered, 14, 15, UIControlEventEditingDidBegin, UIControlEventEditingChanged, UIControlEventEditingDidEnd, UIControlEventEditingDidEndOnExit, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31