From 6f97d4ce40f1049297736bfdfd6f26a5239234ac Mon Sep 17 00:00:00 2001 From: Mark Rickert Date: Wed, 18 Mar 2015 21:25:37 -0400 Subject: [PATCH 1/2] Adds region change delegate methods. --- README.md | 16 ++++++++++++++++ app/test_screens/test_map_screen.rb | 11 +++++++++++ lib/ProMotion/map/map_screen_module.rb | 16 ++++++++++++++++ spec/func_map_screen_spec.rb | 14 ++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/README.md b/README.md index b4ab263..168f812 100644 --- a/README.md +++ b/README.md @@ -310,6 +310,22 @@ end --- +### Delegate callbacks + +These methods (if implemented in your `MapScreen`) will be called when the corresponding `MKMapViewDelegate` method is invoked: + +```ruby +def region_will_change(animated) + # Do something when the region will change +end + +def region_did_change(animated) + # Do something when the region changed +end +``` + +--- + ### CocoaTouch Property Convenience Methods `MKMapView` contains multiple property setters and getters that can be accessed in a more ruby-like syntax: diff --git a/app/test_screens/test_map_screen.rb b/app/test_screens/test_map_screen.rb index a114025..23f3b18 100644 --- a/app/test_screens/test_map_screen.rb +++ b/app/test_screens/test_map_screen.rb @@ -1,5 +1,6 @@ class TestMapScreen < PM::MapScreen attr_accessor :infinite_loop_points, :request_complete, :action_called + attr_accessor :got_region_will_change, :got_region_did_change start_position latitude: 35.090648651123, longitude: -82.965972900391, radius: 4 title "Gorges State Park, NC" @@ -7,6 +8,8 @@ class TestMapScreen < PM::MapScreen def on_load @action_called = false + @got_region_did_change = false + @got_region_will_change = false end def promotion_annotation_data @@ -54,4 +57,12 @@ def my_action @action_called = true end + def region_will_change + @got_region_will_change = true + end + + def region_did_change + @got_region_did_change = true + end + end diff --git a/lib/ProMotion/map/map_screen_module.rb b/lib/ProMotion/map/map_screen_module.rb index 847c224..483d437 100644 --- a/lib/ProMotion/map/map_screen_module.rb +++ b/lib/ProMotion/map/map_screen_module.rb @@ -312,6 +312,22 @@ def mapView(map_view, didUpdateUserLocation:userLocation) end end + def mapView(map_view, regionWillChangeAnimated:animated) + if self.respond_to?("region_will_change:") + region_will_change(animated) + elsif self.respond_to?(:region_will_change) + region_will_change + end + end + + def mapView(map_view, regionDidChangeAnimated:animated) + if self.respond_to?("region_did_change:") + region_did_change(animated) + elsif self.respond_to?(:region_did_change) + region_did_change + end + end + ########## Cocoa touch Ruby counterparts ################# def type diff --git a/spec/func_map_screen_spec.rb b/spec/func_map_screen_spec.rb index 70c4ce3..14723d4 100644 --- a/spec/func_map_screen_spec.rb +++ b/spec/func_map_screen_spec.rb @@ -274,4 +274,18 @@ def add_image_annotation @placemark = nil end end + + it "should call region_will_change" do + map_screen.on_load + map_screen.got_region_will_change.should == false + map_screen.mapView(map_screen.map, regionWillChangeAnimated: true) + map_screen.got_region_will_change.should == true + end + + it "should call region_did_change" do + map_screen.on_load + map_screen.got_region_did_change.should == false + map_screen.mapView(map_screen.map, regionDidChangeAnimated: true) + map_screen.got_region_did_change.should == true + end end From 4d561dcaaa6439e80e9cd5307b8bf76eb6b53614 Mon Sep 17 00:00:00 2001 From: Mark Rickert Date: Tue, 24 Mar 2015 08:58:00 -0400 Subject: [PATCH 2/2] Update region change delegate method names. --- README.md | 10 ++++++++-- app/test_screens/test_map_screen.rb | 14 +++++++------- lib/ProMotion/map/map_screen_module.rb | 16 ++++++++-------- spec/func_map_screen_spec.rb | 12 ++++++------ 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 168f812..2fc0b42 100644 --- a/README.md +++ b/README.md @@ -315,12 +315,18 @@ end These methods (if implemented in your `MapScreen`) will be called when the corresponding `MKMapViewDelegate` method is invoked: ```ruby -def region_will_change(animated) +def will_change_region(animated) # Do something when the region will change + # The animated parameter is optional so you can also define it is simply: + # def will_change_region + # end end -def region_did_change(animated) +def on_change_region(animated) # Do something when the region changed + # The animated parameter is optional so you can also define it is simply: + # def on_change_region + # end end ``` diff --git a/app/test_screens/test_map_screen.rb b/app/test_screens/test_map_screen.rb index 23f3b18..540d7c1 100644 --- a/app/test_screens/test_map_screen.rb +++ b/app/test_screens/test_map_screen.rb @@ -1,6 +1,6 @@ class TestMapScreen < PM::MapScreen attr_accessor :infinite_loop_points, :request_complete, :action_called - attr_accessor :got_region_will_change, :got_region_did_change + attr_accessor :got_will_change_region, :got_on_change_region start_position latitude: 35.090648651123, longitude: -82.965972900391, radius: 4 title "Gorges State Park, NC" @@ -8,8 +8,8 @@ class TestMapScreen < PM::MapScreen def on_load @action_called = false - @got_region_did_change = false - @got_region_will_change = false + @got_will_change_region = false + @got_on_change_region = false end def promotion_annotation_data @@ -57,12 +57,12 @@ def my_action @action_called = true end - def region_will_change - @got_region_will_change = true + def will_change_region + @got_will_change_region = true end - def region_did_change - @got_region_did_change = true + def on_change_region + @got_on_change_region = true end end diff --git a/lib/ProMotion/map/map_screen_module.rb b/lib/ProMotion/map/map_screen_module.rb index 483d437..c4f5c6c 100644 --- a/lib/ProMotion/map/map_screen_module.rb +++ b/lib/ProMotion/map/map_screen_module.rb @@ -313,18 +313,18 @@ def mapView(map_view, didUpdateUserLocation:userLocation) end def mapView(map_view, regionWillChangeAnimated:animated) - if self.respond_to?("region_will_change:") - region_will_change(animated) - elsif self.respond_to?(:region_will_change) - region_will_change + if self.respond_to?("will_change_region:") + will_change_region(animated) + elsif self.respond_to?(:will_change_region) + will_change_region end end def mapView(map_view, regionDidChangeAnimated:animated) - if self.respond_to?("region_did_change:") - region_did_change(animated) - elsif self.respond_to?(:region_did_change) - region_did_change + if self.respond_to?("on_change_region:") + on_change_region(animated) + elsif self.respond_to?(:on_change_region) + on_change_region end end diff --git a/spec/func_map_screen_spec.rb b/spec/func_map_screen_spec.rb index 14723d4..24c2b8e 100644 --- a/spec/func_map_screen_spec.rb +++ b/spec/func_map_screen_spec.rb @@ -275,17 +275,17 @@ def add_image_annotation end end - it "should call region_will_change" do + it "should call will_change_region" do map_screen.on_load - map_screen.got_region_will_change.should == false + map_screen.got_will_change_region.should == false map_screen.mapView(map_screen.map, regionWillChangeAnimated: true) - map_screen.got_region_will_change.should == true + map_screen.got_will_change_region.should == true end - it "should call region_did_change" do + it "should call on_change_region" do map_screen.on_load - map_screen.got_region_did_change.should == false + map_screen.got_on_change_region.should == false map_screen.mapView(map_screen.map, regionDidChangeAnimated: true) - map_screen.got_region_did_change.should == true + map_screen.got_on_change_region.should == true end end