50
RubyMotionを使い倒せ 2012720Ruby Business Commons Eihiro Saishu

Ruby motion勉強会 2012年7月

Embed Size (px)

DESCRIPTION

RBC勉強会資料

Citation preview

Page 1: Ruby motion勉強会 2012年7月

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

Ruby Business CommonsEihiro Saishu

Page 2: Ruby motion勉強会 2012年7月

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

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

素晴らしいRuby言語だけ。

Page 3: Ruby motion勉強会 2012年7月

Ruby Rspec

iconimagesoundetc.

iOS SDKFramework

Libraryetc.

xcode

Simulator

iPhoneiPad

NativeApp

Page 4: Ruby motion勉強会 2012年7月
Page 5: Ruby motion勉強会 2012年7月
Page 6: Ruby motion勉強会 2012年7月
Page 7: Ruby motion勉強会 2012年7月

はじめましょう

Page 8: Ruby motion勉強会 2012年7月
Page 9: Ruby motion勉強会 2012年7月
Page 10: Ruby motion勉強会 2012年7月

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

Objective C to RubyMotion

Page 11: Ruby motion勉強会 2012年7月

RubyMotion Syntax

Page 12: Ruby motion勉強会 2012年7月

iOS SDKを使う

Page 13: Ruby motion勉強会 2012年7月
Page 14: Ruby motion勉強会 2012年7月

UIAlertView

Page 15: Ruby motion勉強会 2012年7月

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

Page 16: Ruby motion勉強会 2012年7月

UIAlertView

2

3

Page 17: Ruby motion勉強会 2012年7月
Page 18: Ruby motion勉強会 2012年7月
Page 19: Ruby motion勉強会 2012年7月

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.

Page 20: Ruby motion勉強会 2012年7月
Page 21: Ruby motion勉強会 2012年7月

Project

Page 22: Ruby motion勉強会 2012年7月

Timerを作る

Page 23: Ruby motion勉強会 2012年7月

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

ゼロになると音楽がなる

Page 24: Ruby motion勉強会 2012年7月

Start

終わったら音楽

時間設定

カウントダウン

Stop

時間設定

カウントダウン

UIPickerView"Timer Setting"

UILabel"Count Down

Label"

UIButton"Start Button"

Page 25: Ruby motion勉強会 2012年7月

ベース画面を作る

Page 26: Ruby motion勉強会 2012年7月

rake spec

Page 27: Ruby motion勉強会 2012年7月

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

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

まずこいつ

Page 28: Ruby motion勉強会 2012年7月
Page 29: Ruby motion勉強会 2012年7月
Page 30: Ruby motion勉強会 2012年7月

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

Page 31: Ruby motion勉強会 2012年7月
Page 32: Ruby motion勉強会 2012年7月

class MyFirstViewController < UIViewControllerend

Page 33: Ruby motion勉強会 2012年7月
Page 34: Ruby motion勉強会 2012年7月

Test!

Page 35: Ruby motion勉強会 2012年7月

rspec

Page 36: Ruby motion勉強会 2012年7月

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

Page 37: Ruby motion勉強会 2012年7月

Label

Page 38: Ruby motion勉強会 2012年7月

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

Page 39: Ruby motion勉強会 2012年7月

Button

Page 40: Ruby motion勉強会 2012年7月

@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)

Page 41: Ruby motion勉強会 2012年7月

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

Page 42: Ruby motion勉強会 2012年7月

# -*- 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

Page 43: Ruby motion勉強会 2012年7月

PickerView

Page 44: Ruby motion勉強会 2012年7月

data source

numberOfComponentsnumberOfRows

Data

delegate

選択したData

Page 45: Ruby motion勉強会 2012年7月

@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)

Page 46: Ruby motion勉強会 2012年7月

# 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

Page 47: Ruby motion勉強会 2012年7月

Storyboard

Page 48: Ruby motion勉強会 2012年7月

Motion Live

Page 49: Ruby motion勉強会 2012年7月

Test Flight

Page 50: Ruby motion勉強会 2012年7月

RubyMotion and TestFlight