class GyroDrivenImageViewController

Attributes

motion_rate[RW]
shake_threshold[RW]
update_interval[RW]

Public Instance Methods

activate_timer_control() click to toggle source
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 47
def activate_timer_control
  queue  = Dispatch::Queue.current
  @timer = Dispatch::Source.timer(self.update_interval, self.update_interval, 0.0, queue) do |source|
    begin
      periodic_timer
    ensure
      # source.cancel!
    end
  end 
end
content_exceeds_available_space?() click to toggle source
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 89
def content_exceeds_available_space?
  @scroll_view.contentSize.width > self.width
end
device_motion_available() click to toggle source
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 27
def device_motion_available
  @motion_manager.deviceMotion
end
fit_image_to_height() click to toggle source
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 93
def fit_image_to_height
  super
  set_up_gyro if gyro_available
end
get_scroll_offset_towards_left(roll) click to toggle source
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 70
def get_scroll_offset_towards_left roll
  current_offset = @scroll_view.contentOffset.x
  new_offset = current_offset - (current_offset * roll)
  new_offset = 0 if new_offset < 0
  new_offset
end
get_scroll_offset_towards_right(roll) click to toggle source
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 77
def get_scroll_offset_towards_right roll
  current_offset = @scroll_view.contentOffset.x
  width = @scroll_view.contentSize.width - self.width
  new_offset = current_offset - ((width - current_offset) * roll)
  new_offset = width if new_offset > width
  new_offset
end
gyro_available() click to toggle source
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 19
def gyro_available
  @motion_manager.isGyroAvailable
end
gyro_requirements_met?() click to toggle source
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 85
def gyro_requirements_met?
  device_motion_available && significant_tilt && @scroll_view
end
initialize_gyro_setup() click to toggle source
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 36
def initialize_gyro_setup
  @motion_manager.showsDeviceMovementDisplay = true
  @motion_manager.deviceMotionUpdateInterval = self.update_interval
  @motion_manager.startDeviceMotionUpdates
end
periodic_timer() click to toggle source
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 58
def periodic_timer
  if gyro_requirements_met? && content_exceeds_available_space?
    roll = @motion_manager.deviceMotion.rotationRate.y * self.motion_rate
    if roll > 0
      new_offset = get_scroll_offset_towards_left roll
    elsif roll < 0
      new_offset = get_scroll_offset_towards_right roll
    end
    @scroll_view.setContentOffset([new_offset, @scroll_view.contentOffset.y], animated: false)
  end
end
set_up_gyro() click to toggle source
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 31
def set_up_gyro
  initialize_gyro_setup
  activate_timer_control
end
significant_tilt() click to toggle source
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 23
def significant_tilt
  @motion_manager.deviceMotion.rotationRate.y.abs >= self.shake_threshold
end
tear_down_gyro() click to toggle source
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 42
def tear_down_gyro
  @timer.cancel!
  @motion_manager.stopDeviceMotionUpdates
end
viewDidAppear(animated) click to toggle source
Calls superclass method
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 9
def viewDidAppear animated
  super
  set_up_gyro if gyro_available
end
viewDidLoad() click to toggle source
Calls superclass method
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 4
def viewDidLoad
  super
  @motion_manager = CMMotionManager.alloc.init
end
viewWillDisappear(animated) click to toggle source
Calls superclass method
# File lib/motion-gyro-image-controller/gyro_driven_image_view_controller.rb, line 14
def viewWillDisappear(animated)
  super
  tear_down_gyro if gyro_available
end