gpt4 book ai didi

objective-c - FSInteractiveMap Objective-C 到 Swfit 的翻译

转载 作者:行者123 更新时间:2023-11-30 11:53:28 26 4
gpt4 key购买 nike

我想使用FSInteractiveMap swift 但文档只是 Objective-C,我无法快速翻译点击处理程序。

 NSDictionary* data = @{    @"asia" : @12,
@"australia" : @2,
@"north_america" : @5,
@"south_america" : @14,
@"africa" : @5,
@"europe" : @20
};

FSInteractiveMapView* map = [[FSInteractiveMapView alloc] initWithFrame:self.view.frame];

[map loadMap:@"world-continents-low" withData:data colorAxis:@[[UIColor lightGrayColor], [UIColor darkGrayColor]]];

快速合作:

    let map: FSInteractiveMapView = FSInteractiveMapView()
map.frame = self.view.frame
var mapData = [String: Int]()
mapData["IR-01"] = 0
mapData["IR-02"] = 10
var mapColors = [UIColor]()
mapColors.append(UIColor(red:0.26, green:0.112, blue:0.0, alpha:1.0))
mapColors.append(UIColor(red:0.45, green:0.132, blue:0.0, alpha:1.0))


map.loadMap("iranHigh", withData:mapData, colorAxis:mapColors)
view.addSubview(map)
view.setNeedsDisplay()

它工作正常,但我无法添加点击处理程序

这是 Objective-C 中的文档:

FSInteractiveMapView* map = [[FSInteractiveMapView alloc] initWithFrame:CGRectMake(16, 96, self.view.frame.size.width - 32, 500)];
[map loadMap:@"usa-low" withColors:nil];

[map setClickHandler:^(NSString* identifier, CAShapeLayer* layer) {
if(_oldClickedLayer) {
_oldClickedLayer.zPosition = 0;
_oldClickedLayer.shadowOpacity = 0;
}

_oldClickedLayer = layer;

// We set a simple effect on the layer clicked to highlight it
layer.zPosition = 10;
layer.shadowOpacity = 0.5;
layer.shadowColor = [UIColor blackColor].CGColor;
layer.shadowRadius = 5;
layer.shadowOffset = CGSizeMake(0, 0);
}];

或者:

[map setClickHandler:^(NSString* identifier, CAShapeLayer* layer) {
self.detailDescriptionLabel.text = [NSString stringWithFormat:@"Continent clicked: %@", identifier];}];


如何在 Swift 中做到这一点?

最佳答案

添加一些代码,看起来像这样

    override func viewDidLoad() {
super.viewDidLoad()

let map: FSInteractiveMapView = FSInteractiveMapView()
weak var oldClickedLayer = CAShapeLayer()

var mapData = [String: Int]()
mapData["asia"] = 12
mapData["australia"] = 2
mapData["north_america"] = 5
mapData["south_america"] = 14
mapData["africa"] = 5
mapData["europe"] = 20

var mapColors = [UIColor]()
mapColors.append(UIColor.lightGray)
mapColors.append(UIColor.darkGray)
map.frame = self.view.frame
map.clickHandler = {(identifier: String? , _ layer: CAShapeLayer?) -> Void in
if (oldClickedLayer != nil) {
oldClickedLayer?.zPosition = 0
oldClickedLayer?.shadowOpacity = 0
}
oldClickedLayer = layer
// We set a simple effect on the layer clicked to highlight it
layer?.zPosition = 10
layer?.shadowOpacity = 0.5
layer?.shadowColor = UIColor.black.cgColor
layer?.shadowRadius = 5
layer?.shadowOffset = CGSize(width: 0, height: 0)

print("clicked")
}
let mapName: String! = String("world-continents-low")
map.loadMap(mapName, withData:mapData, colorAxis:mapColors)

view.addSubview(map)
view.setNeedsDisplay()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

这对我有用。不要忘记导入库

关于objective-c - FSInteractiveMap Objective-C 到 Swfit 的翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48095708/

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