Method: UIView#tumble_in

Defined in:
lib/sugarcube-animations/uiview.rb

#tumble_in(options = {}, more_options = {}, &after) ⇒ Object

Moves the view on screen while slowly rotating it.

Based on https://github.com/warrenm/AHAlertView/blob/master/AHAlertView/AHAlertView.m



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/sugarcube-animations/uiview.rb', line 499

def tumble_in(options={}, more_options={}, &after)
  if options.is_a? Numeric
    default_duration = options
    options = more_options
    side = options[:side] || :left
  elsif options.is_a? Symbol
    side = options
    options = more_options
    default_duration = 0.3
  else
    default_duration = 0.3
    side = options[:side] || :left
  end

  case side
    when :left
      angle = -Math::PI/4
    when :right
      angle = Math::PI/4
    else
      raise "Unknown direction #{side.inspect}"
  end

  reset_transform = self.transform
  reset_center = self.center

  options[:duration] ||= default_duration
  unless options.key?(:options) || options.key?(:curve)
    options[:options] = UIView.sugarcube_animation_options(curve: UIViewAnimationOptionCurveEaseOut)
  end
  options[:after] = after

  window = UIApplication.sharedApplication.windows[0]
  top = self.convertPoint([0, 0], toView:nil).y
  height = window.frame.size.height - top
  offset = CGPoint.new(0, height * -1.5)
  offset = CGPointApplyAffineTransform(offset, self.transform)
  self.transform = CGAffineTransformConcat(self.transform, CGAffineTransformMakeRotation(angle))
  self.center = CGPointMake(self.center.x + offset.x, self.center.y + offset.y)

  self.animate(options) do
    self.transform = reset_transform
    self.center = reset_center
  end
end