Permalink
Browse files

Simplify view anchoring design by removing the use of anchor flags

  • Loading branch information...
1 parent 7fc5026 commit 3a7d97369a50ef19fe8d51740ac1f9e3274a2852 @seanhotw seanhotw committed Oct 15, 2012
Showing with 52 additions and 58 deletions.
  1. +3 −3 README.md
  2. +1 −0 Rakefile
  3. +10 −10 app/simple_view_controller.rb
  4. +36 −42 lib/simple_view/extensions/ui_view.rb
  5. +2 −3 spec/simple_view_spec.rb
View
@@ -33,9 +33,9 @@ class YourViewController < UIViewController
def viewDidLoad
setup content_view do
- label width: 200, height: 20, text: "Choose your lucky word", color: "#eee"
+ label top:0, left: 0, width: 200, height: 20, text: "Choose your lucky word", color: "#eee"
image_view top: 50, left: 50, right: 50, image: "sample.jpg"
- toolbar anchors: [:bottom]
+ toolbar left: 0, right: 0, bottom: 0
end
end
end
@@ -157,7 +157,7 @@ Position the view without doing a lot of calculation
````ruby
setup view do
- toolbar bottom: 10, left: 10, right: 10, anchors: [:bottom]
+ toolbar bottom: 10, left: 10, right: 10
end
````
View
@@ -6,6 +6,7 @@ require 'motion/project'
Motion::Project::App.setup do |app|
app.name = 'SimpleViewDemo'
+ app.deployment_target = '4.3'
app.files += Dir.glob(File.join(File.dirname(__FILE__), 'lib/simple_view/*.rb'))
app.files.unshift(File.join(File.dirname(__FILE__), 'lib/simple_view/layout.rb'))
@@ -9,19 +9,19 @@ def viewDidLoad
setup view, controller: self do
controller.title = "SimpleView Demo"
- rect styles: :square, backgroundColor: "#000", anchors: [:top, :left, :bottom, :right]
+ rect styles: :square, backgroundColor: "#000", top: 0, left: 0, bottom: 0, right: 0
- rect styles: :square, backgroundColor: "#990000", anchors: [:top, :left]
- rect styles: :square, backgroundColor: "#993300", anchors: [:top]
- rect styles: :square, backgroundColor: "#CC9900", anchors: [:top, :right]
+ rect styles: :square, backgroundColor: "#990000", top: 0, left: 0
+ rect styles: :square, backgroundColor: "#993300", top: 0
+ rect styles: :square, backgroundColor: "#CC9900", top: 0, right: 0
- rect styles: :square, backgroundColor: "#006600", anchors: [:left]
- rect styles: :square, backgroundColor: "#336666", anchors: []
- rect styles: :square, backgroundColor: "#0033FF", anchors: [:right]
+ rect styles: :square, backgroundColor: "#006600", left: 0
+ rect styles: :square, backgroundColor: "#336666"
+ rect styles: :square, backgroundColor: "#0033FF", right: 0
- rect styles: :square, backgroundColor: "#000099", anchors: [:bottom, :left]
- rect styles: :square, backgroundColor: "#660099", anchors: [:bottom]
- rect styles: :square, backgroundColor: "#990066", anchors: [:bottom, :right]
+ rect styles: :square, backgroundColor: "#000099", bottom: 0, left: 0
+ rect styles: :square, backgroundColor: "#660099", bottom: 0
+ rect styles: :square, backgroundColor: "#990066", bottom: 0, right: 0
end
end
end
@@ -1,6 +1,6 @@
module SimpleView
module UIView
- attr_accessor :name, :bottom, :right, :anchors
+ attr_accessor :name, :top, :left, :bottom, :right
def find name
subviews.each do |subview|
@@ -26,60 +26,54 @@ def closest name
view
end
+ def top
+ @top.to_i
+ end
+
+ def left
+ @left.to_i
+ end
+
+ def bottom
+ @bottom.to_i
+ end
+
+ def right
+ @right.to_i
+ end
+
def invalidate_size
f = self.frame
max_width = superview ? superview.bounds.size.width : 0
max_height = superview ? superview.bounds.size.height : 0
- @anchors ||= [:top, :left]
- anchor_top = @anchors.include?(:top)
- anchor_left = @anchors.include?(:left)
- anchor_bottom = @anchors.include?(:bottom)
- anchor_right = @anchors.include?(:right)
-
self.autoresizingMask = UIViewAutoresizingNone
- self.autoresizingMask |= UIViewAutoresizingFlexibleTopMargin unless anchor_top
- self.autoresizingMask |= UIViewAutoresizingFlexibleLeftMargin unless anchor_left
- self.autoresizingMask |= UIViewAutoresizingFlexibleBottomMargin unless anchor_bottom
- self.autoresizingMask |= UIViewAutoresizingFlexibleRightMargin unless anchor_right
- self.autoresizingMask |= UIViewAutoresizingFlexibleWidth if anchor_left && anchor_right
- self.autoresizingMask |= UIViewAutoresizingFlexibleHeight if anchor_top && anchor_bottom
-
- if (anchor_left && anchor_right) || (anchor_left && !@right.nil?)
- f.size.width = max_width - self.left - @right.to_i
- elsif anchor_right
- f.origin.x = max_width - self.width - @right.to_i
- elsif !anchor_left && !anchor_right
- f.origin.x = max_width / 2 - f.size.width / 2
+ self.autoresizingMask |= UIViewAutoresizingFlexibleTopMargin if @top.nil?
+ self.autoresizingMask |= UIViewAutoresizingFlexibleLeftMargin if @left.nil?
+ self.autoresizingMask |= UIViewAutoresizingFlexibleBottomMargin if @bottom.nil?
+ self.autoresizingMask |= UIViewAutoresizingFlexibleRightMargin if @right.nil?
+ self.autoresizingMask |= UIViewAutoresizingFlexibleWidth if @left && @right
+ self.autoresizingMask |= UIViewAutoresizingFlexibleHeight if @top && @bottom
+
+ if @left && @right
+ f.size.width = max_width - left - right
+ elsif @right
+ f.origin.x = max_width - width - right
+ elsif @left.nil? && @right.nil?
+ f.origin.x = max_width / 2 - width / 2
end
- if (anchor_top && anchor_bottom) || (anchor_top && !@bottom.nil?)
- f.size.height = max_height - self.top - @bottom.to_i
- elsif anchor_bottom
- f.origin.y = max_height - self.height - @bottom.to_i
- elsif !anchor_top && !anchor_bottom
- f.origin.y = max_height / 2 - f.size.height / 2
+ if @top && @bottom
+ f.size.height = max_height - top - bottom
+ elsif @bottom
+ f.origin.y = max_height - height - bottom
+ elsif @top.nil? && @bottom.nil?
+ f.origin.y = max_height / 2 - height / 2
end
self.frame = f
end
- def top
- self.frame.origin.y
- end
-
- def setTop value
- self.frame = [[self.frame.origin.x, value], [self.frame.size.width, self.frame.size.height]]
- end
-
- def left
- self.frame.origin.x
- end
-
- def setLeft value
- self.frame = [[value, self.frame.origin.y], [self.frame.size.width, self.frame.size.height]]
- end
-
def width
self.frame.size.width
end
View
@@ -40,13 +40,12 @@ class DummyController
describe "#add" do
it "should add view with options" do
- frame = CGRectMake(0, 0, 10, 10)
alpha = 0.5
backgroundColor = UIColor.redColor
proxy = SimpleView::ViewProxy.new
- view = proxy.add UIView, frame: frame, alpha: alpha, backgroundColor: backgroundColor
- view.frame.should == frame
+ view = proxy.add UIView, top: 0, left: 0, width: 10, height: 10, alpha: alpha, backgroundColor: backgroundColor
+ view.frame.should == CGRectMake(0, 0, 10, 10)
view.alpha.should == alpha
view.backgroundColor.should == backgroundColor
end

0 comments on commit 3a7d973

Please sign in to comment.