Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to RubyMotion

Avatar for jin jin
July 30, 2014

Introduction to RubyMotion

Tinkerbox Lunch Talk

Avatar for jin

jin

July 30, 2014
Tweet

Other Decks in Programming

Transcript

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

    begin require 'bundler' Bundler.require rescue LoadError end ! Motion::Project::App.setup do |app| # Use `rake config' to see complete project settings. app.name = 'iosapp' end
  2. class AppDelegate def application(application, didFinishLaunchingWithOptions:launchOptions) true end end app/app_delegate.rb #import

    "AppDelegate.h" ! @implementation AppDelegate ! - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { return YES; } ! @end
  3. ViewControllers are structured in a stack bar_controller = BarViewController.alloc.init navigationController.pushViewController(bar_controller,

    animated: true) # do something.. navigationController.popViewController(bar_controller, animated: true)
  4. > label = adjust(53) => UILabel(#1e8b9ce0, [[210.0, 5.0], [30.0, 25.0]],

    text: nil), child of MenuItemView(#1e8b6200) stylename: :news_count ! > label.frame.origin.x += 10 => 220.0 ! > right 5 => [[225.0, 5.0], [30.0, 25.0]] # => [x, y], [width, height] ! > label.text = "Hello World" => "Hello World"
  5. https://github.com/rubymotion/sugarcube :center.nsalignment       #  =>  NSTextAlignmentCenter   !

    :upside_down.uiorientation       #  =>  UIDeviceOrientationPortraitUpsideDown button.on(:touch_up_outside,  :touch_cancel)   {  |event|      puts  event.inspect      #  my_code...   } Constants Events 1.nth    #  =>  'st'   2.nth    #  =>  'nd'   3.nth    #  =>  'rd' Ordinals image.rotate(:left)   image.rotate(:right)   image.rotate(:flip)   image.rotate(45.degrees) Images + tree, and many more
  6. https://github.com/rubymotion/BubbleWrap >  App.alert("BubbleWrap  is  awesome!")   #  creates  and  shows

     an  alert  message. >  Device.ios_version   #  "6.0"   >  Device.retina?   #  false   >  Device.screen.width   #  320 BW::Location.get_once(desired_accuracy:  :three_kilometers,  ...)  do  |result|      if  result.is_a?(CLLocation)          p  result.coordinate.latitude          p  result.coordinate.longitude      else          p  "ERROR:  #{result[:error]}"      end   end button.when(UIControlEventTouchUpInside)  do      self.view.backgroundColor  =  UIColor.redColor   end Alerts Device Information Location UI Events
  7. https://github.com/colinta/teacup #  classic  Cocoa/UIKit   def  viewDidLoad      self.view.backgroundColor

     =  UIColor.grayColor      #                  ^.............^   end   ! #  in  Teacup   def  viewDidLoad      self.stylesheet  =  :main_menu      self.view.stylename  =  :root   end   ! Teacup::Stylesheet.new  :main_menu  do      style  :root,          backgroundColor:  UIColor.grayColor,          frame:  [[20,  300],  [50,  20]]   end
  8. https://github.com/usepropeller/afmotion “AFMotion is a thin RubyMotion wrapper for AFNetworking, the

    absolute best networking library on iOS.” AFMotion::HTTP.get("http://google.com")  do  |result|      p  result.body   end Normal requests: Web images: AFMotion::Image.get(“https://www.google.com/images/srpr/ logo3w.png")  do  |result|          image_view  =  UIImageView.alloc.initWithImage(result.object)   end
  9. https://github.com/tkadauke/motion-resource class  User  <  MotionResource::Base      attr_accessor  :id  

    !    has_many  :posts   !    self.collection_url  =  "users"      self.member_url  =  "users/:id"   end   ! class  Post  <  MotionResource::Base      attr_accessor  :id      attribute  :user_id,  :title,  :text   !    belongs_to  :user   !    self.collection_url  =  "users/:user_id/posts"      self.member_url  =  "users/:user_id/posts/:id"   end   MotionResource::Base.root_url  =  "http://localhost:3000/"   User.find(1)  do  |user|      user.posts  do  |posts|          puts  posts.inspect      end   end