Skip to content

Commit

Permalink
add Timer sample
Browse files Browse the repository at this point in the history
  • Loading branch information
lrz committed Apr 24, 2012
1 parent b685ac7 commit 0ad23af
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Timer/README
@@ -0,0 +1,3 @@
Timer.app

This sample demonstrates the following concepts: Timer, Label, Button.
7 changes: 7 additions & 0 deletions Timer/Rakefile
@@ -0,0 +1,7 @@
$:.unshift("/Library/Motion/lib")
require 'motion/project'

Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = 'Timer'
end
9 changes: 9 additions & 0 deletions Timer/app/app_delegate.rb
@@ -0,0 +1,9 @@
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
@window.rootViewController = TimerController.alloc.init
@window.rootViewController.wantsFullScreenLayout = true
@window.makeKeyAndVisible
true
end
end
36 changes: 36 additions & 0 deletions Timer/app/timer_controller.rb
@@ -0,0 +1,36 @@
class TimerController < UIViewController
def viewDidLoad
margin = 20

@state = UILabel.new
@state.font = UIFont.systemFontOfSize(30)
@state.text = 'Tap to start'
@state.textAlignment = UITextAlignmentCenter
@state.textColor = UIColor.whiteColor
@state.backgroundColor = UIColor.clearColor
@state.frame = [[margin, 200], [view.frame.size.width - margin * 2, 40]]
view.addSubview(@state)

@action = UIButton.buttonWithType(UIButtonTypeRoundedRect)
@action.setTitle('Start', forState:UIControlStateNormal)
@action.setTitle('Stop', forState:UIControlStateSelected)
@action.addTarget(self, action:'actionTapped', forControlEvents:UIControlEventTouchUpInside)
@action.frame = [[margin, 260], [view.frame.size.width - margin * 2, 40]]
view.addSubview(@action)
end

def actionTapped
if @timer
@timer.invalidate
@timer = nil
else
@duration = 0
@timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target:self, selector:'timerFired', userInfo:nil, repeats:true)
end
@action.selected = !@action.selected?
end

def timerFired
@state.text = "%.1f" % (@duration += 0.1)
end
end
9 changes: 9 additions & 0 deletions Timer/spec/main_spec.rb
@@ -0,0 +1,9 @@
describe "Application 'Timer'" do
before do
@app = UIApplication.sharedApplication
end

it "has one window" do
@app.windows.size.should == 1
end
end

0 comments on commit 0ad23af

Please sign in to comment.