diff --git a/README.md b/README.md index b4ab263..2fc0b42 100644 --- a/README.md +++ b/README.md @@ -310,6 +310,28 @@ end --- +### Delegate callbacks + +These methods (if implemented in your `MapScreen`) will be called when the corresponding `MKMapViewDelegate` method is invoked: + +```ruby +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 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 +``` + +--- + ### 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..540d7c1 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_will_change_region, :got_on_change_region 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_will_change_region = false + @got_on_change_region = false end def promotion_annotation_data @@ -54,4 +57,12 @@ def my_action @action_called = true end + def will_change_region + @got_will_change_region = true + end + + 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 847c224..c4f5c6c 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?("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?("on_change_region:") + on_change_region(animated) + elsif self.respond_to?(:on_change_region) + on_change_region + 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..24c2b8e 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 will_change_region" do + map_screen.on_load + map_screen.got_will_change_region.should == false + map_screen.mapView(map_screen.map, regionWillChangeAnimated: true) + map_screen.got_will_change_region.should == true + end + + it "should call on_change_region" do + map_screen.on_load + map_screen.got_on_change_region.should == false + map_screen.mapView(map_screen.map, regionDidChangeAnimated: true) + map_screen.got_on_change_region.should == true + end end