samples/camera/app/controllers/camera_controller.rb in bubble-wrap-1.1.5 vs samples/camera/app/controllers/camera_controller.rb in bubble-wrap-1.2.0.pre
- old
+ new
@@ -1,61 +1,46 @@
class CameraController < UIViewController
+ attr_accessor :buttons
+ def init
+ super.tap do
+ @buttons = []
+ end
+ end
+
def viewDidLoad
super
- @buttons = []
+ self.view.addSubview(build_button("Library", :any))
+ self.view.addSubview(build_button("Front", :front)) if BW::Device.camera.front?
+ self.view.addSubview(build_button("Rear", :rear)) if BW::Device.camera.rear?
+ end
- @library = UIButton.buttonWithType(UIButtonTypeRoundedRect)
- @library.setTitle("Library", forState:UIControlStateNormal)
- @library.sizeToFit
- @library.when UIControlEventTouchUpInside do
- BW::Device.camera.any.picture(media_types: [:image]) do |result|
- image_view = UIImageView.alloc.initWithImage(result[:original_image])
- add_image_view(image_view)
- end
- end
- self.view.addSubview(@library)
- @buttons << @library
+ def build_button(title, camera_method)
+ button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
+ button.setTitle(title, forState:UIControlStateNormal)
+ button.sizeToFit
- if BW::Device.camera.front?
- @front = UIButton.buttonWithType(UIButtonTypeRoundedRect)
- @front.setTitle("Front", forState:UIControlStateNormal)
- @front.sizeToFit
- last_button = @buttons.last
- @front.frame = [[last_button.frame.origin.x, last_button.frame.origin.y + last_button.frame.size.height + 10], @front.frame.size]
- @front.when UIControlEventTouchUpInside do
- BW::Device.camera.front.picture(media_types: [:image]) do |result|
- image_view = UIImageView.alloc.initWithImage(result[:original_image])
- add_image_view(image_view)
- end
- end
- self.view.addSubview(@front)
- @buttons << @front
- end
+ rect = self.buttons.empty? ? CGRectMake(0, 0, 0, 0) : self.buttons.last.frame
- if BW::Device.camera.rear?
- @rear = UIButton.buttonWithType(UIButtonTypeRoundedRect)
- @rear.setTitle("Read", forState:UIControlStateNormal)
- @rear.sizeToFit
- last_button = @buttons.last
- @rear.frame = [[last_button.frame.origin.x, last_button.frame.origin.y + last_button.frame.size.height + 10], @rear.frame.size]
- @rear.when UIControlEventTouchUpInside do
- BW::Device.camera.rear.picture(media_types: [:image]) do |result|
- image_view = UIImageView.alloc.initWithImage(result[:original_image])
- add_image_view(image_view)
- end
+ button.frame = [[rect.origin.x, rect.origin.y + rect.size.height + 10], button.frame.size]
+
+ button.when UIControlEventTouchUpInside do
+ BW::Device.camera.send(camera_method).picture(media_types: [:image]) do |result|
+ image_view = build_image_view(result[:original_image])
+ self.view.addSubview(image_view)
+
+ self.buttons.each { |button| self.view.bringSubviewToFront(button) }
end
- self.view.addSubview(@rear)
- @buttons << @rear
end
+
+ self.buttons << button
+ button
end
- def add_image_view(image_view)
- image_view.frame = [CGPointZero, self.view.frame.size]
+ def build_image_view(image)
+ image_view = UIImageView.alloc.initWithImage(image)
+ image_view.frame = [CGPointZero, self.view.frame.size]
image_view.center = [self.view.frame.size.width / 2, self.view.frame.size.height / 2]
- self.view.addSubview(image_view)
- @buttons.each do |button|
- self.view.bringSubviewToFront(button)
- end
+ image_view
end
end