ruby motion勉強会 2012年7月

Post on 15-May-2015

1.543 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

RBC勉強会資料

TRANSCRIPT

RubyMotionを使い倒せ2012年7月20日

Ruby Business CommonsEihiro Saishu

RubyMotionは、iOSのための革新的な製品です。

RubyMotionを使えば、iPhoneやiPad向けのネイティブアプリを素早く開発・テストすることができます。使うのはみんなが知っていて大好きな

素晴らしいRuby言語だけ。

Ruby Rspec

iconimagesoundetc.

iOS SDKFramework

Libraryetc.

xcode

Simulator

iPhoneiPad

NativeApp

はじめましょう

[string drawAtPoint:point withFont:font];string.drawAtPoint(point, withFont:font)

Objective C to RubyMotion

RubyMotion Syntax

iOS SDKを使う

UIAlertView

1. UIAlertView object作成2. メッセージ設定3. 表示

UIAlertView

2

3

Although you can use new to instantiate an Objective-C class, I strongly recommend you use alloc.init or related constructors defined by the class.

Project

Timerを作る

時間をセットしてカウントダウン

ゼロになると音楽がなる

Start

終わったら音楽

時間設定

カウントダウン

Stop

時間設定

カウントダウン

UIPickerView"Timer Setting"

UILabel"Count Down

Label"

UIButton"Start Button"

ベース画面を作る

rake spec

describe "Application 'MyFirst'" do before do @app = UIApplication.sharedApplication end

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

まずこいつ

@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) @window.rootViewController = MyFirstViewController.alloc.init @window.rootViewController.wantsFullScreenLayout = true @window.makeKeyAndVisible

class MyFirstViewController < UIViewControllerend

Test!

rspec

describe "Timer Main Window" do

tests MyFirstViewController

it "has one pickerview" do views(UIPickerView).size.should == 1 end it "has Label" do views(UILabel).size.should >= 1 view("Count Down Label").text.should == "Set the Time" end it "has one button" do views(UIButton).size.should == 1 end it "can be time up" do tap("Start Button") view("Count Down Label").text.should == "Time Up" endend

Label

def viewDidLoad @state = UILabel.alloc.init @state.accessibilityLabel = "Count Down Label" @state.font = UIFont.systemFontOfSize(30) @state.text = 'Set the Time' @state.textAlignment = UITextAlignmentCenter @state.textColor = UIColor.whiteColor @state.backgroundColor = UIColor.clearColor @state.frame = [[20, 250], [view.frame.size.width - 20 * 2, 40]] view.addSubview(@state) end

Button

@time = 0

@button = UIButton.buttonWithType(UIButtonTypeRoundedRect) @button.accessibilityLabel = "Start Button" @button.frame = [[40, 310], [view.frame.size.width - 40 * 2, 60]] @button.setTitle("Start", forState:UIControlStateNormal) @button.addTarget(self,

action:"onButton", forControlEvents:UIControlEventTouchDown)

view.addSubview(@button)

url = NSURL.fileURLWithPath(NSBundle.mainBundle.pathForResource("181", ofType: "mp3")) er = Pointer.new(:object) @player = AVAudioPlayer.alloc.initWithContentsOfURL(url, error: er)

def onButton if @timer @timer.invalidate @timer = nil else @timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target:self,

selector:'timerFired', userInfo:nil, repeats:true) end end

def timerFired @state.text = "%.1f" % (@time -= 0.1) timeExpired if @time <= 0 end def timeExpired @state.text = "Time Up" @timer.invalidate @timer = nil @player.play end

# -*- coding: utf-8 -*-$:.unshift("/Library/RubyMotion/lib")require 'motion/project'

Motion::Project::App.setup do |app| # Use `rake config' to see complete project settings. app.name = 'MyFirst' app.frameworks += ['AVFoundation', 'AudioToolbox']end

PickerView

data source

numberOfComponentsnumberOfRows

Data

delegate

選択したData

@picker = UIPickerView.alloc.init @picker.frame = [[20, 20], [view.frame.size.width - 20 * 2, 162]] @picker.accessibilityLabel = "Timer Setting" @picker.showsSelectionIndicator = true @picker.delegate = self @picker.dataSource = self view.addSubview(@picker)

# data source for picker view def numberOfComponentsInPickerView(pickerView) 1 end def pickerView(pickerView, numberOfRowsInComponent:component) 10 end def pickerView(pickerView, titleForRow:row,

forComponent:component) "#{row+1} seconds" end # delegate for picker view def pickerView(pickerView, didSelectRow:row, inComponent:component) @time = row + 1 @state.text = @time.to_s end

Storyboard

Motion Live

Test Flight

RubyMotion and TestFlight

top related