gradient-view 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ /build/
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'sugarcube'
4
+ gem 'gradient-view', path: '.'
5
+
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gradient-view (0.1.0)
5
+ rake
6
+ sugarcube
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ rake (0.9.2.2)
12
+ sugarcube (0.9.3)
13
+ rake
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ gradient-view!
20
+ sugarcube
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ GradientView
2
+ -----
3
+
4
+ Define a startColor and finalColor, and an angle, and this view will create a
5
+ gradient with those colors that transistions in that given direction.
6
+
7
+ TODO
8
+ ---
9
+
10
+ - gemify
11
+ - add tests
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ $:.unshift('/Library/RubyMotion/lib')
2
+ require 'motion/project'
3
+ require 'bundler'
4
+ Bundler.require
5
+
6
+
7
+ Motion::Project::App.setup do |app|
8
+ # Use `rake config' to see complete project settings.
9
+ app.name = 'gradient_view'
10
+ app.identifier = 'com.colinta.gradient_view'
11
+ end
@@ -0,0 +1,24 @@
1
+ class AppDelegate
2
+
3
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
4
+ return true if RUBYMOTION_ENV == 'test'
5
+
6
+ @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
7
+
8
+ ctlr = UIViewController.new
9
+ ctlr.view.frame = @window.bounds
10
+
11
+ gradient = GradientView.new
12
+ gradient.frame = ctlr.view.bounds
13
+ gradient.startColor = UIColor.whiteColor
14
+ gradient.finalColor = UIColor.blackColor
15
+
16
+ ctlr.view.addSubview(gradient)
17
+
18
+ @window.rootViewController = ctlr
19
+ @window.makeKeyAndVisible
20
+
21
+ true
22
+ end
23
+
24
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/gradient_view/version.rb', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'gradient-view'
6
+ gem.version = GradientView::Version
7
+
8
+ gem.authors = ['Colin Thomas-Arnold']
9
+ gem.email = ['colinta@gmail.com']
10
+ gem.summary = %{A view that can draw a gradient.}
11
+ gem.description = <<-DESC
12
+ == Description
13
+
14
+ Sometimes you need a gradient.
15
+ DESC
16
+
17
+ gem.homepage = 'https://github.com/colinta/gradient_view'
18
+
19
+ gem.files = `git ls-files`.split($\)
20
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
21
+ gem.test_files = gem.files.grep(%r{^spec/})
22
+
23
+ gem.require_paths = ['lib']
24
+
25
+ gem.add_dependency 'rake'
26
+ gem.add_dependency 'sugarcube'
27
+
28
+ end
@@ -0,0 +1,23 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "The gradient-view gem must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+
6
+ Motion::Project::App.setup do |app|
7
+ # scans app.files until it finds app/ (the default)
8
+ # if found, it inserts just before those files, otherwise it will insert to
9
+ # the end of the list
10
+ insert_point = 0
11
+ app.files.each_index do |index|
12
+ file = app.files[index]
13
+ if file =~ /^(?:\.\/)?app\//
14
+ # found app/, so stop looking
15
+ break
16
+ end
17
+ insert_point = index + 1
18
+ end
19
+
20
+ Dir.glob(File.join(File.dirname(__FILE__), 'gradient_view/**/*.rb')).reverse.each do |file|
21
+ app.files.insert(insert_point, file)
22
+ end
23
+ end
@@ -0,0 +1,66 @@
1
+ class GradientView < UIView
2
+
3
+ attr_accessor :startColor
4
+ attr_accessor :finalColor
5
+ attr_accessor :type # :linear, :radial
6
+
7
+ # for linear gradient:
8
+ attr_accessor :angle # 0 - PI
9
+
10
+
11
+ def initWithFrame(frame)
12
+ super.tap do
13
+ self.startColor = :white
14
+ self.finalColor = :black
15
+ self.angle = Math::PI / 2
16
+ self.type = :linear
17
+ end
18
+ end
19
+
20
+ def drawRect(rect)
21
+ case self.type
22
+ when :linear
23
+ drawLinearGradient
24
+ when :radial
25
+ drawRadialGradient
26
+ end
27
+ end
28
+
29
+ def drawLinearGradient
30
+ context = UIGraphicsGetCurrentContext()
31
+ color_space = CGColorSpaceCreateDeviceRGB()
32
+
33
+ w = self.frame.size.width
34
+ h = self.frame.size.height
35
+ if w == 0 or h == 0
36
+ return
37
+ end
38
+
39
+ the_start_color = self.startColor
40
+ the_final_color = self.finalColor
41
+ angle = self.angle
42
+ angle %= 2 * Math::PI
43
+ if angle > Math::PI
44
+ angle = Math::PI - angle % Math::PI
45
+ the_start_color, the_final_color = the_final_color, the_start_color
46
+ end
47
+
48
+ radius = Math.hypot(w/2, h/2)
49
+ r_angle = Math.atan2(h/2, w/2)
50
+ inner_angle = self.angle - r_angle
51
+ l = radius * Math.cos(inner_angle)
52
+
53
+ center = SugarCube::CoreGraphics::Point(w/2, h/2)
54
+ start_point = center + SugarCube::CoreGraphics::Point(l * Math.cos(angle - Math::PI),
55
+ l * Math.sin(angle - Math::PI))
56
+ final_point = center + SugarCube::CoreGraphics::Point(l * Math.cos(angle),
57
+ l * Math.sin(angle))
58
+ gradient = CGGradientCreateWithColors(color_space, [the_start_color.uicolor.CGColor, the_final_color.uicolor.CGColor],
59
+ [0, 1].to_pointer(:float))
60
+ CGContextDrawLinearGradient(context, gradient, start_point, final_point, 0)
61
+ end
62
+
63
+ def drawRadialGradient
64
+ end
65
+
66
+ end
@@ -0,0 +1,3 @@
1
+ class GradientView
2
+ Version = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gradient-view
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Colin Thomas-Arnold
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sugarcube
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: ! '== Description
47
+
48
+
49
+ Sometimes you need a gradient.
50
+
51
+ '
52
+ email:
53
+ - colinta@gmail.com
54
+ executables: []
55
+ extensions: []
56
+ extra_rdoc_files: []
57
+ files:
58
+ - .gitignore
59
+ - Gemfile
60
+ - Gemfile.lock
61
+ - README.md
62
+ - Rakefile
63
+ - app/app_delegate.rb
64
+ - gradient_view.gemspec
65
+ - lib/gradient-view.rb
66
+ - lib/gradient_view/gradient_view.rb
67
+ - lib/gradient_view/version.rb
68
+ homepage: https://github.com/colinta/gradient_view
69
+ licenses: []
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.19
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: A view that can draw a gradient.
92
+ test_files: []