我想在swift中使用FSInteractiveMap 但是文档只是Objective-C而我无法翻译swift中的单击处理程序。
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中做到这一点?
答案 0 :(得分:0)
添加一些代码,看起来像那样
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.
}
对我有用。 别忘了导入库