gpt4 book ai didi

iphone - iOS 自定义 MKAnnotation 对象类似 MKUserLocation 动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:05:21 30 4
gpt4 key购买 nike

我想将 MKAnnotationView 图钉放在 map 上,并让图钉像 MKUserLocation 动画一样动画(带有圆形波浪的蓝点出现)

如何实现这种类型的动画?我需要采取哪些步骤?

谢谢!

最佳答案

像任何 UIView 一样为其设置动画!

自定义内容的子类 MKAnnotationView。 不要通过重复的 addAnnotation 调用来为其设置动画!这是错误的

你可以像对待任何其他 View 一样对待anotationView..

这里是一些具有图像闪烁动画的代码——这段代码应该可以帮助您入门。

(不是很漂亮,但正是您需要的东西!)

#import "DDViewController.h"
#import <MapKit/MapKit.h>
#import <QuartzCore/QuartzCore.h>

@interface DummyAnnotation : NSObject<MKAnnotation>
@end
@implementation DummyAnnotation
- (CLLocationCoordinate2D)coordinate { return CLLocationCoordinate2DMake(51, 10); }
- (NSString *)title { return @"Dummy"; }
@end

@interface DummyAnnotationView : MKAnnotationView
@end
@implementation DummyAnnotationView
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
imageView.animationImages = @[[UIImage imageNamed:@"1.gif"],[UIImage imageNamed:@"2.gif"]];
imageView.animationDuration = 5;
[imageView startAnimating];
[self addSubview:imageView];

return self;
}
@end

@interface DDViewController() <MKMapViewDelegate>
@end
@implementation DDViewController

- (MKMapView*)mapView {
return (MKMapView*)self.view;
}

- (void)viewDidLoad
{
[super viewDidLoad];

//init a region
MKCoordinateRegion region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(51.0, 10.0), MKCoordinateSpanMake(2.0, 2.0));
[self.mapView setRegion:region animated:NO];

//add a dumy pin
DummyAnnotation *ann = [[DummyAnnotation alloc] init];
[self.mapView addAnnotation:ann];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if([annotation isKindOfClass:[DummyAnnotation class]]) {
DummyAnnotationView *view = (id)[mapView dequeueReusableAnnotationViewWithIdentifier:@"animated"];
if(!view)
view =[[DummyAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier:@"animated"];
view.bounds = CGRectMake(0, 0, 59, 59);
view.backgroundColor = [UIColor purpleColor];

//
//Animate it like any UIView!
//

CABasicAnimation *theAnimation;

//within the animation we will adjust the "opacity"
//value of the layer
theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
//animation lasts 0.4 seconds
theAnimation.duration=0.4;
//and it repeats forever
theAnimation.repeatCount= HUGE_VALF;
//we want a reverse animation
theAnimation.autoreverses=YES;
//justify the opacity as you like (1=fully visible, 0=unvisible)
theAnimation.fromValue=[NSNumber numberWithFloat:1.0];
theAnimation.toValue=[NSNumber numberWithFloat:0.1];

//Assign the animation to your UIImage layer and the
//animation will start immediately
[view.layer addAnimation:theAnimation forKey:@"animateOpacity"];

return view;
}
return nil;
}

//---

@end

将工作示例上传到我的保管箱(它最终会消失,但上面的代码除了图像资源和 iOS 应用程序的默认样板代码之外都是其他代码)::https://dl.dropbox.com/u/3753090/test.zip

关于iphone - iOS 自定义 MKAnnotation 对象类似 MKUserLocation 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14916974/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com