Module: MotionPrime::HasStyles

Included in:
DrawSectionMixin, ViewStyler
Defined in:
motion-prime/helpers/has_styles.rb

Instance Method Summary collapse

Instance Method Details

#prepare_gradient(options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'motion-prime/helpers/has_styles.rb', line 3

def prepare_gradient(options)
  colors = options[:colors].map(&:uicolor).map(&:cgcolor)
  locations = options[:locations] if options[:locations]
  type = options[:type].to_s

  if self.is_a?(ViewStyler)
    gradient = CAGradientLayer.layer
    if type == 'horizontal'
      gradient.startPoint = CGPointMake(0, 0.5)
      gradient.endPoint = CGPointMake(1.0, 0.5)
    end
    gradient.frame = if options[:frame_width]
      CGRectMake(options[:frame_x].to_f, options[:frame_y].to_f, options[:frame_width].to_f, options[:frame_height].to_f)
    else
      options[:parent_frame] || CGRectZero
    end

    gradient.colors = colors
    gradient.locations = locations
  else
    color_space = CGColorSpaceCreateDeviceRGB()
    locations_pointer = Pointer.new(:float, 2)
    locations.each_with_index { |loc, id| locations_pointer[id] = loc }
    gradient = CGGradientCreateWithColors(color_space, colors, locations_pointer)
    # CGColorSpaceRelease(color_space)
  end
  gradient
end