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

标签 objective-c swift dictionary svg

我想使用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/

相关文章:

ios - 如何将 objective-c block 存储到 NSDictionary 中?

python - 如何在for循环中创建一个新字典?

javascript - 激活/停用 leaflet.js 上的图层

ios - 我应该使用哪个压缩库来在 Objective-C 中正确组装有效的 XLSX 文件?

objective-c - 如何在Mac上分别录制音频和视频?

php - 快速以肖像模式拍摄照片并上传到服务器 - 上传的图像显示方向错误

swift - ios9 缓存 NSData?

swift - 为什么泛型特化会在泛型函数中丢失

php - 在列表中分组 - php

ios - 计算字体大小以适合框架 - 核心文本 - NSAttributedString - iOS