2014-09-30 36 views
-2

我想在swift中使用這個庫。它是用Obj-C編寫的。在swift中等效的Obj-C alloc語句

我添加標題和橋接和等,現在,我不知道什麼是相當於本聲明:

 //Color did change block declaration 
    NKOColorPickerDidChangeColorBlock colorDidChangeBlock = ^(UIColor *color){ 
     //Your code handling a color change in the picker view. 
    }; 

    NKOColorPickerView *colorPickerView = [[NKOColorPickerView alloc] initWithFrame:CGRectMake(0, 0, 300, 340) color:[UIColor blueColor] andDidChangeColorBlock:colorDidChangeBlock]; 

    //Add color picker to your view 
    [self.view addSubview:colorPickerView]; 

回答

4
let colorPickerView = NKOColorPickerView(frame: CGRect(x: 0, y: 0, width: 300, height: 340), color: UIColor.blueColor()) { (color) -> Void in 
    // Your code handling a color change in the picker view. 
} 

view.addSubview(colorPickerView) 

var colorDidChangeBlock: NKOColorPickerDidChangeColorBlock = { (color) in 
    // Your code handling a color change in the picker view. 
} 

let colorPickerView = NKOColorPickerView(frame: CGRect(x: 0, y: 0, width: 300, height: 340), color: UIColor.blueColor(), andDidChangeColorBlock: colorDidChangeBlock) 
view.addSubview(colorPickerView) 

我更喜歡第一辦法。它更清晰,更易於閱讀。

+0

哇,太棒了!謝謝! – 2014-09-30 14:32:10