Class: GradientView
- Inherits:
-
UIView
- Object
- UIView
- GradientView
- Defined in:
- lib/gradient-view/version.rb,
lib/gradient-view/gradient_view.rb
Constant Summary collapse
- Version =
'0.1.3'
Instance Attribute Summary collapse
-
#angle ⇒ Object
for linear gradient:.
-
#finalColor ⇒ Object
Returns the value of attribute finalColor.
-
#gradientCenter ⇒ Object
for radial gradient:.
-
#startColor ⇒ Object
Returns the value of attribute startColor.
-
#type ⇒ Object
:linear, :radial.
Instance Method Summary collapse
- #drawLinearGradient ⇒ Object
- #drawRadialGradient ⇒ Object
- #drawRect(rect) ⇒ Object
- #initWithFrame(frame) ⇒ Object
Instance Attribute Details
#angle ⇒ Object
for linear gradient:
7 8 9 |
# File 'lib/gradient-view/gradient_view.rb', line 7 def angle @angle end |
#finalColor ⇒ Object
Returns the value of attribute finalColor.
3 4 5 |
# File 'lib/gradient-view/gradient_view.rb', line 3 def finalColor @finalColor end |
#gradientCenter ⇒ Object
for radial gradient:
9 10 11 |
# File 'lib/gradient-view/gradient_view.rb', line 9 def gradientCenter @gradientCenter end |
#startColor ⇒ Object
Returns the value of attribute startColor.
2 3 4 |
# File 'lib/gradient-view/gradient_view.rb', line 2 def startColor @startColor end |
#type ⇒ Object
:linear, :radial
4 5 6 |
# File 'lib/gradient-view/gradient_view.rb', line 4 def type @type end |
Instance Method Details
#drawLinearGradient ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/gradient-view/gradient_view.rb', line 30 def drawLinearGradient w = CGRectGetWidth(self.frame) h = CGRectGetHeight(self.frame) if w == 0 or h == 0 return end context = UIGraphicsGetCurrentContext() color_space = CGColorSpaceCreateDeviceRGB() the_start_color = self.startColor the_final_color = self.finalColor angle = self.angle angle %= 2 * Math::PI if angle > Math::PI angle = Math::PI - angle % Math::PI the_start_color, the_final_color = the_final_color, the_start_color end radius = Math.hypot(w/2, h/2) r_angle = Math.atan2(h/2, w/2) inner_angle = self.angle - r_angle l = radius * Math.cos(inner_angle) center = CGPoint.new(w/2, h/2) start_point = center + CGPoint.new(l * Math.cos(angle - Math::PI), l * Math.sin(angle - Math::PI)) final_point = center + CGPoint.new(l * Math.cos(angle), l * Math.sin(angle)) start_color = self.startColor start_color = start_color.uicolor unless start_color.is_a? UIColor final_color = self.finalColor final_color = final_color.uicolor unless final_color.is_a? UIColor gradient = CGGradientCreateWithColors(color_space, [start_color.CGColor, final_color.CGColor], [0, 1].to_pointer(:float)) CGContextDrawLinearGradient(context, gradient, start_point, final_point, 0) end |
#drawRadialGradient ⇒ Object
70 71 |
# File 'lib/gradient-view/gradient_view.rb', line 70 def drawRadialGradient end |
#drawRect(rect) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/gradient-view/gradient_view.rb', line 21 def drawRect(rect) case self.type when :linear drawLinearGradient when :radial drawRadialGradient end end |
#initWithFrame(frame) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/gradient-view/gradient_view.rb', line 11 def initWithFrame(frame) super.tap do self.startColor = :white self.finalColor = :black self.angle = Math::PI / 2 self.type = :linear self.backgroundColor = UIColor.clearColor end end |