Skip to content

Instantly share code, notes, and snippets.

@SecondReality
Created May 26, 2012 21:52
Show Gist options
  • Save SecondReality/2795449 to your computer and use it in GitHub Desktop.
Save SecondReality/2795449 to your computer and use it in GitHub Desktop.
UITapTapTap
//
// UITapTapTap.h
// Cat Toys
//
// Created by Steven Mark Rose on 01/11/2010.
// Copyright 2010 Second Reality. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface UITapTapTap : UIControl
{
@private
UIButton * button;
int stage;
float stageTime;
NSTimer * timer;
}
- (id)initWithFrame:(CGRect)aRect;
- (void)updateTimeDelta:(float)time;
@end
//
// UITapTapTap.m
// Cat Toys
//
// Created by Steven Mark Rose on 01/11/2010.
// Copyright 2010 Second Reality. All rights reserved.
//
#import "UITapTapTap.h"
#import <QuartzCore/QuartzCore.h>
@implementation UITapTapTap
- (id)initWithFrame:(CGRect)aRect
{
if(self=[super initWithFrame:aRect])
{
stage=0;
stageTime=0.0f;
// Add an image for the background:
button =[UIButton buttonWithType: UIButtonTypeCustom];
[[button layer] setCornerRadius:8.0f];
[[button layer] setMasksToBounds:YES];
[button setTitle:@"Tap three times for menu" forState: UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[[button layer] setBackgroundColor:[[UIColor blackColor] CGColor]];
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
CGPoint titleCenter = {self.bounds.size.width/2, self.bounds.size.height/2};
button.center=titleCenter;
CGRect bounds={0,0, 300, 55};
button.bounds=bounds;
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
timer=[NSTimer scheduledTimerWithTimeInterval:0.25f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
}
return self;
}
-(void)dealloc
{
[button release];
[timer invalidate];
[super dealloc];
}
- (void)buttonPressed:(id)sender
{
stage++;
if(stage==1)
{
[button setTitle:@"Tap twice more!" forState: UIControlStateNormal];
stageTime=0.0f;
}
else if(stage==2)
{
[button setTitle:@"Tap once more!" forState: UIControlStateNormal];
stageTime=0.0f;
}
else if(stage==3)
{
[super sendActionsForControlEvents:UIControlEventValueChanged];
stageTime=0.0f;
}
}
- (void)updateTimeDelta:(float)time
{
stageTime+=time;
if(stageTime>5.0f)
{
[button setTitle:@"Tap three times for menu" forState: UIControlStateNormal];
stage=0;
stageTime=0.0f;
}
}
- (void)updateCounter:(NSTimer *)timer
{
[self updateTimeDelta:1.0f];
}
@end
@SecondReality
Copy link
Author

This class is used for the menu button on the game screen. You have to press it three times within a time period otherwise the it will not activate. The purpose is to prevent cats from getting to the main menu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment