双击缩放时注释视图消失的问题

发布于 2024-08-06 01:59:30 字数 3951 浏览 4 评论 0原文

我在 iPhone 上的 MapKit 中遇到了注释视图问题。我设法在地图上绘制自定义注释视图 - 没问题。我什至设法在拖动或缩放后重新绘制它们。但是,在某些情况下重绘不起作用:例如双击缩放。

我附加了一些代码,在地图上的特定位置绘制了一些矩形,当我使用两根手指手势进行缩放时,一切正常(即重新绘制矩形)。但是,当我双击时,矩形就会消失。更奇怪的是,所有方法都按其应有的顺序调用,最后,甚至调用了drawRect - 但没有绘制矩形。

所以这里是代码,请自己尝试 - 两根手指缩放有效,但双击缩放不起作用:

PlaygroundViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface PlaygroundViewController : UIViewController <MKMapViewDelegate>{
 MKMapView *mapView_;
 NSMutableDictionary* myViews_;
}

@end

PlaygroundViewController.m

#import "PlaygroundViewController.h"
#import "Territory.h"
#import "TerritoryView.h"

@implementation PlaygroundViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 mapView_=[[MKMapView alloc] initWithFrame:self.view.bounds];
 [self.view insertSubview:mapView_ atIndex:0];
 mapView_.delegate = self;
 [mapView_ setMapType:MKMapTypeStandard];
    [mapView_ setZoomEnabled:YES];
    [mapView_ setScrollEnabled:YES];
 myViews_ = [[NSMutableDictionary alloc] init];
 for (int i = 0; i < 10; i++ ) {
  Territory *territory;
  territory = [[[Territory alloc] init] autorelease];
     territory.latitude_ = 40 + i;
  territory.longitude_ = -122 + i;
  [mapView_ addAnnotation:territory];

 }
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
 MKAnnotationView* territoryView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"Territory"];
 if (!territoryView){
  territoryView = [[[TerritoryView alloc] initWithAnnotation:annotation reuseIdentifier:@"Territory"] autorelease];
  Territory* currentTerritory = (Territory*) annotation;
  [myViews_ setObject:territoryView forKey:currentTerritory.territoryID_];
 }  
    else{
  territoryView.annotation = annotation;
 }  
 return territoryView; 
}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
 for (NSObject* key in [myViews_ allKeys]) {
  TerritoryView* territoryView = [myViews_ objectForKey:key];
  [territoryView initRedraw];
 }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    [super dealloc];
}

Territory.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>


@interface Territory : NSObject <MKAnnotation> {
 float latitude_;
 float longitude_;
 NSString* territoryID_;
}

@property (nonatomic) float latitude_;
@property (nonatomic) float longitude_;
@property (nonatomic, retain) NSString* territoryID_;


@end

Territory.m

#import "Territory.h"

@implementation Territory

@synthesize latitude_;
@synthesize longitude_;
@synthesize territoryID_;


- (CLLocationCoordinate2D)coordinate {
 CLLocationCoordinate2D coord_ = {self.latitude_, self.longitude_};
 return coord_;
}

-(id) init {
 if (self = [super init]) {
  self.territoryID_ = [NSString stringWithFormat:@"%p", self];
 }
 return self;
}


@end

TerritoryView.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface TerritoryView : MKAnnotationView {

}

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
- (void)initRedraw;

@end

TerritoryView.m

#import "TerritoryView.h"

@implementation TerritoryView

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    if ([super initWithAnnotation:annotation reuseIdentifier:@"Territory"]) {
  self.initRedraw;
    }
    return self;
}

- (void)initRedraw {
 self.frame = CGRectMake(0,0,40,40);
 [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
 NSLog(@"in draw rect");
}

@end

任何帮助表示赞赏。这是压缩的项目:链接

I've ran into a problem with annotation views in the MapKit on the iPhone. I manage to draw custom annotation views on the map - no problem there. I even manage to redraw them after dragging or zooming. However, there are cases where the redrawing does not work: an example would be double-tap zoom.

I attach some code where I draw a few rectangles at specific locations on the map, and when I zoom using a two finger gesture, everything works fine (i.e. the rectangles are redrawn). However, when I double tap, the rectangles disappear. What's even stranger is that all methods get called in the order that they should, and in the end, even the drawRect gets called - but the rectangles are not drawn.

So here's the code, please try for yourself - two finger zooming works, but double-tap zooming doesn't:

PlaygroundViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface PlaygroundViewController : UIViewController <MKMapViewDelegate>{
 MKMapView *mapView_;
 NSMutableDictionary* myViews_;
}

@end

PlaygroundViewController.m

#import "PlaygroundViewController.h"
#import "Territory.h"
#import "TerritoryView.h"

@implementation PlaygroundViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 mapView_=[[MKMapView alloc] initWithFrame:self.view.bounds];
 [self.view insertSubview:mapView_ atIndex:0];
 mapView_.delegate = self;
 [mapView_ setMapType:MKMapTypeStandard];
    [mapView_ setZoomEnabled:YES];
    [mapView_ setScrollEnabled:YES];
 myViews_ = [[NSMutableDictionary alloc] init];
 for (int i = 0; i < 10; i++ ) {
  Territory *territory;
  territory = [[[Territory alloc] init] autorelease];
     territory.latitude_ = 40 + i;
  territory.longitude_ = -122 + i;
  [mapView_ addAnnotation:territory];

 }
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
 MKAnnotationView* territoryView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"Territory"];
 if (!territoryView){
  territoryView = [[[TerritoryView alloc] initWithAnnotation:annotation reuseIdentifier:@"Territory"] autorelease];
  Territory* currentTerritory = (Territory*) annotation;
  [myViews_ setObject:territoryView forKey:currentTerritory.territoryID_];
 }  
    else{
  territoryView.annotation = annotation;
 }  
 return territoryView; 
}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
 for (NSObject* key in [myViews_ allKeys]) {
  TerritoryView* territoryView = [myViews_ objectForKey:key];
  [territoryView initRedraw];
 }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    [super dealloc];
}

Territory.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>


@interface Territory : NSObject <MKAnnotation> {
 float latitude_;
 float longitude_;
 NSString* territoryID_;
}

@property (nonatomic) float latitude_;
@property (nonatomic) float longitude_;
@property (nonatomic, retain) NSString* territoryID_;


@end

Territory.m

#import "Territory.h"

@implementation Territory

@synthesize latitude_;
@synthesize longitude_;
@synthesize territoryID_;


- (CLLocationCoordinate2D)coordinate {
 CLLocationCoordinate2D coord_ = {self.latitude_, self.longitude_};
 return coord_;
}

-(id) init {
 if (self = [super init]) {
  self.territoryID_ = [NSString stringWithFormat:@"%p", self];
 }
 return self;
}


@end

TerritoryView.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface TerritoryView : MKAnnotationView {

}

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
- (void)initRedraw;

@end

TerritoryView.m

#import "TerritoryView.h"

@implementation TerritoryView

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    if ([super initWithAnnotation:annotation reuseIdentifier:@"Territory"]) {
  self.initRedraw;
    }
    return self;
}

- (void)initRedraw {
 self.frame = CGRectMake(0,0,40,40);
 [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
 NSLog(@"in draw rect");
}

@end

Any help is appreciated. Here's the zipped project: link

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坦然微笑 2024-08-13 01:59:30

请记住,框架的原点位于其父级的坐标系中,因此将其设置为零可能会将其移出屏幕。我怀疑它之所以能发挥作用,是因为在大多数情况下它会在你背后重置,但在失败的情况下却不会。

代替:
self.frame = CGRectMake(0,0,40,40);

和:
self.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y,40,40);

Keep in mind that the frame's origin is in the coordinate system of its parent, so setting it to zero is probably putting it off screen. I suspect that the reason it ever works at all is the it's getting reset behind your back in most situations, but not in those where it's failing.

replace:
self.frame = CGRectMake(0,0,40,40);

with:
self.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y,40,40);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文