控制的MKMapView在iOS6的动画速度 [英] Controlling the animation speed of MKMapView in iOS6
问题描述
我试图按照汽车在地图视图。
I'm trying to follow a car on a map view.
这code应该动画车以相同的速度地图,从而使注解视图始终出现在中心:
This code should animate the car and the map with the same speed, so that the annotation view always appears in the center:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration: 1.0];
[UIView setAnimationBeginsFromCurrentState:YES];
[car setCoordinate:coord];
[mapView setCenterCoordinate:coord];
[UIView commitAnimations];
它在iOS的5好工作在iOS 6中地图上没有动画了,但车上没有动画。
我试过 [图形页面setCenterCoordinate:共同动画:是]
,但我无法控制动画的速度。它总是以默认的持续时间(0.2秒)动画。
I tried [mapView setCenterCoordinate:co animated:YES]
, but then I cannot control the animation speed. It will always animate with the default duration (0.2s).
推荐答案
这似乎是不可能使用的MKMapView
这些方法时,控制动画速度的iOS 6
It seems it is not possible to control the animation speed in iOS 6 when using these methods in MKMapView
:
- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated;
- (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated;
- (void)setVisibleMapRect:(MKMapRect)mapRect animated:(BOOL)animate;
- (void)setVisibleMapRect:(MKMapRect)mapRect edgePadding:(UIEdgeInsets)insets animated:(BOOL)animate;
不过,我发现,有一个持续时间参数的私有方法。所以,我解决我的问题,通过继承的MKMapView并重写私有方法(一种只能在iOS6的存在):
However I found a private method that has a duration parameter. So I solved my issue by subclassing MKMapView and overriding the private method (which only exists in iOS6):
MyMapView.h
#import <MapKit/MapKit.h>
@interface MyMapView : MKMapView
@property(nonatomic) BOOL overridesAnimationDuration;
@property(nonatomic) NSTimeInterval mapAnimationDuration;
@end
MyMapView.m
@interface MKMapView (Private)
- (void)_setZoomScale:(float)scale centerMapPoint:(CLLocationCoordinate2D)center duration:(double)d animationType:(int)animType;
@end
@implementation MyMapView
- (void)_setZoomScale:(float)scale centerMapPoint:(CLLocationCoordinate2D)center duration:(double)d animationType:(int)animType
{
if (_overridesAnimationDuration) {
d = _mapAnimationDuration;
}
[super _setZoomScale:scale centerMapPoint:center duration:d animationType:animType];
}
@end
我添加了2个属性,使您可以覆盖默认的动画时间。
要使用它的区域变化,设置 mapAnimationDuration
来所需的持续时间之前设置 overridesAnimationDuration
来YES,然后切换 overridesAnimationDuration
来NO在委托调用图形页面:regionWillChangeAnimated:
I've added 2 properties that allow you to override the default animation time.
To use it set overridesAnimationDuration
to YES before a region change, set mapAnimationDuration
to the desired duration and switch overridesAnimationDuration
to NO in the delegate call mapView:regionWillChangeAnimated:
.
请注意,这可能不会在将来的IOS版本工作,因为它是私有的API。苹果可以删除或更改此方法。
Note, this might not work in future iOS versions because it is private API. Apple can remove or change this method.
这篇关于控制的MKMapView在iOS6的动画速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!