gpt4 book ai didi

objective-c - 从 MKAnnotation 对象更改图钉颜色

转载 作者:行者123 更新时间:2023-12-01 17:58:28 25 4
gpt4 key购买 nike

我努力实现这一点。我有一个类,它是 NSObject 的子类,表示 map 上的注释。在该类中,当用户按下注释时,我有一个警报 View 。我想做的是,在用户从警报中按确定后,我想将图钉的颜色从红色更改为绿色。

下面是我的 MKAnnotation 类...

。H

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


@interface BuildingViewController : NSObject <MKAnnotation, MapMenuDelegate>{

NSString *name;
NSString *description;
CLLocationCoordinate2D location;
NSString *owner; /*user object*/

}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *owner;
@property (nonatomic, retain) IBOutlet MapMenu *menu;

- (id) initBuildingWithName: (NSString *) _name andCoordinates: (CLLocationCoordinate2D) _location
shortDescription:(NSString *)_description andOwner:(NSString *)_owner inDistance: (NSInteger) _distance;

- (void) addMenuOnView: (MKMapView*) mapView;
- (void) hideMenuFromView;
- (void) setAttackEnable: (BOOL) attack;
- (void) attackTo: (BuildingViewController*) selectedBuilding;

@end

.m
#import "BuildingViewController.h"
#import "CountDownTimer.h"

@implementation BuildingViewController{

MapMenuItem* starMenuItem1, *starMenuItem2,
*starMenuItem3, *starMenuItem4;
NSInteger distance;
MKMapView* parentView;
}

@synthesize title, subtitle, coordinate, owner, menu = _menu;

- (id) initBuildingWithName:(NSString *)_name andCoordinates:(CLLocationCoordinate2D)_location
shortDescription:(NSString *)_description andOwner:(NSString *)_owner inDistance:(NSInteger)_distance{

self = [super init];
title = _name;
coordinate = _location;
subtitle = _description;
owner = _owner;
distance = (int)round(_distance);

UIImage *storyMenuItemImage = [UIImage imageNamed:@"bg-menuitem.png"];
UIImage *storyMenuItemImagePressed = [UIImage imageNamed:@"bg-menuitem-highlighted.png"];

UIImage *starImage = [UIImage imageNamed:@"icon-star.png"];

starMenuItem1 = [[MapMenuItem alloc] initWithImage:storyMenuItemImage highlightedImage:storyMenuItemImagePressed
contentImage:starImage highContentImage:nil isDisable:NO];

starMenuItem2 = [[MapMenuItem alloc] initWithImage:storyMenuItemImage highlightedImage:storyMenuItemImagePressed
contentImage:starImage highContentImage:nil isDisable:NO];

starMenuItem3 = [[MapMenuItem alloc] initWithImage:storyMenuItemImage highlightedImage:storyMenuItemImagePressed
contentImage:starImage highContentImage:nil isDisable:NO];

starMenuItem4 = [[MapMenuItem alloc] initWithImage:storyMenuItemImage highlightedImage:storyMenuItemImagePressed
contentImage:starImage highContentImage:nil isDisable:NO];

if (distance > 10){
[self setAttackEnable:NO];
starMenuItem1.disable = YES;
}

NSArray *menus = [NSArray arrayWithObjects:starMenuItem1, starMenuItem2, starMenuItem3, starMenuItem4, nil];

_menu = [[MapMenu alloc] initWithFrame:self.menu.bounds menus:menus];
_menu.delegate = self;

return self;
}

- (void) addMenuOnView:(MKMapView *)mapView{

parentView = mapView;
[parentView addSubview: _menu];
}

- (void) hideMenuFromView{

[_menu removeFromSuperview];
}

- (void) setAttackEnable:(BOOL)attack{

if (attack) {
[starMenuItem1 setHighlighted:NO];
}else{
[starMenuItem1 setHighlighted:YES];
[starMenuItem1 setImage:[UIImage imageNamed:@"bg-menuitem-highlighted.png"]];
}
}

- (void)MapMenu:(MapMenu *)menu didSelectButton:(NSInteger)index{

NSLog(@"Select the index : %d\n",index);
NSLog(@"%@", self.owner);

if (index == 0) {
if (self.owner == nil && distance < 10) {
CountDownTimer* countDown = [[CountDownTimer alloc]init];
[countDown startTimerOn:parentView];
[self performSelector:@selector(attackTo:) withObject:nil afterDelay:20.0];

}
else if (self.owner == @"Player_1")
NSLog(@"You have already occupy this building with name, %@", self.title);
}
}

- (void) attackTo: (BuildingViewController*) selectedBuilding{

self.owner = @"Player_1";
[self showMessage:@"Success" withContent:@"You are the new owner of the building."];
//NSLog(@"Building has a new owner with name, %@", self.owner);
}

- (void) showMessage: (NSString*) msgTitle withContent: (NSString*) content{

UIAlertView *message = [[UIAlertView alloc] initWithTitle:msgTitle
message:content
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

if (buttonIndex == 0) {

//I want to change the pin colour here.
}
}

@end

我在父 View (mapView)上添加图钉的行收到警告: Sending 'MKPinAnnotationView *__strong' to parameter of incompatible type 'id<MKAnnotation>'当我运行应用程序时,按确定时它会崩溃。我不知道我做错了什么。

提前致谢。

最佳答案

我正在试一试,不确定它是否会起作用。

在注释中添加一个属性:

@property (nonatomic) BOOL annotationWasSelected;

这将设置为 TRUE当用户关闭 UIAlertView 时(将其初始化为 FALSE )。

所以用那个替换你的代码:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

if (buttonIndex == 0) {

_annotationWasSelected = TRUE;

// Remove and add the annotation so it's re-drawn
[parentView removeAnnotation:self];
[parentView addAnnotation:self];
}
}

在你的 mapView 的委托(delegate)中,你应该做这样的事情:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

static NSString *identifier = @"MyAnnotation";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

YourAnnotationClass *myAnnotation = (YourAnnotationClass*) annotation;

// If a new annotation is created
if (annotationView == nil) {

annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier];

// Annotation's color
if (myAnnotation.annotationWasSelected) {
annotationView.pinColor = MKPinAnnotationColorGreen;
}
else {
annotationView.pinColor = MKPinAnnotationColorRed;
}

} else {

annotationView.annotation = annotation;

// Annotation's color
if (myAnnotation.annotationWasSelected) {
annotationView.pinColor = MKPinAnnotationColorGreen;
}
else {
annotationView.pinColor = MKPinAnnotationColorRed;
}
}

return annotationView;
}

您必须将此代码与现有代码合并。

同样,我不完全确定此代码,现在无法对其进行测试。让我知道它是否有帮助。

关于objective-c - 从 MKAnnotation 对象更改图钉颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13514624/

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