DRKonamiCode 1.1.1

DRKonamiCode 1.1.1

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Feb 2015

Maintained by Danny Ricciotti.



  • By
  • Danny Ricciotti

DRKonamiCode

Konami code gesture recognizer for iOS. The recognizer is a subclass of UIGestureRecognizer has can be used in the same way as any other recognizer. Swipe gestures correspond to the Up/Down/Left/Right parts of the sequence. An optional feature allows you to implement a custom B+A+Enter action.

Contact me via Twitter @topwobble

Using DRKonamiCode in your App

Add the gesture recognizer to a UIView using the following code.

#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+Unlock (OPTIONAL)

Optionally, you can require the user to enter B+A+Enter in order for the gesture to be recognized. You will need to implement the DRKonamiGestureProtocol which has required methods that let your UI respond to the request for the A, B, or Enter action. If you are not using the B+A+Enter feature than you do not need to set the recognizer's delegate.

- (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.
}

DRKonamiGestureProtocol

The DRKonamiGestureProtocol protocol is required only if you are using the B+A+Enter feature. The recognizer will inform its delegate when the B+A+Enter sequence is needed and when the sequence is no longer needed (due to gesture failing or succeeding).

Tips

  • TIP 1: NSLog() statements are disabled inside of DRKonamiGestureRecognizer.m. You can enable them at the top of the file and then the console will print the konami state.
  • TIP 2: Practice actually doing the konami gesture with the sample app. Some people have a hard time figuring out how to actually get it going.