为 iOS 提供的 Konami 代码手势识别器。识别器是 UIGestureRecognizer 的子类,可以像任何其他识别器一样使用。滑动手势对应于序列的上/下/左/右部分。一个可选功能允许您实现自定义 B+A+Enter 操作。
通过 Twitter 联系我 @topwobble
使用以下代码将手势识别器添加到 UIView 中。
#import "DRKonamiGestureRecognizer.h"
- (void)addKonami
{
konami = [[DRKonamiGestureRecognizer alloc] initWithTarget:self action:@selector(_konamiHappened:)];
[self.view addGestureRecognizer:konami];
}
- (void)_konamiHappened:(DRKonamiGestureRecognizer *)recognizer
{
// NOTE: Test the state to make sure the recognizer is finished.
if ( [recognizer konamiState] == DRKonamiGestureStateRecognized ) {
NSLog(@"Konami Code Recognized!");
}
}
可选地,您可以让用户输入 B+A+Enter 以识别该手势。您需要实现 DRKonamiGestureProtocol,它具有必要的方法,允许您的 UI 对 A、B 或 Enter 行为的请求做出响应。如果您不使用 B+A+Enter 功能,则不需要设置识别器的代理。
- (void)addKonami
{
konami = [[DRKonamiGestureRecognizer alloc] initWithTarget:self action:@selector(_konamiHappened:)];
[konami setKonamiDelegate:self];
[konami setRequiresABEnterToUnlock:YES];
[self.view addGestureRecognizer:konami];
}
- (void)_konamiHappened:(DRKonamiGestureRecognizer *)recognizer
{
NSLog(@"Konami Code Recognized!");
}
#pragma mark -
#pragma mark DRKonamiGestureProtocol
- (void)DRKonamiGestureRecognizerNeedsABEnterSequence:(DRKonamiGestureRecognizer*)gesture
{
/// your code here.
}
- (void)DRKonamiGestureRecognizer:(DRKonamiGestureRecognizer*)gesture didFinishNeedingABEnterSequenceWithError:(BOOL)error
{
/// your code here.
}
仅在您使用 B+A+Enter 功能时需要 DRKonamiGestureProtocol 协议。识别器会在需要 B+A+Enter 序列时通知其代理,并且当序列不再需要时(由于手势失败或成功)也会通知代理。