4 xcode cocoa gesture nsbutton swift
我有这个代码生成多个NSButton:
var height = 0
var width = 0
var ar : Array<NSButton> = []
var storage = NSUserDefaults.standardUserDefaults()
height = storage.integerForKey("mwHeight")
width = storage.integerForKey("mwWidth")
var x = 0
var y = 0
var k = 1
for i in 1...height {
for j in 1...width {
var but = NSButton(frame: NSRect(x: x, y: y + 78, width: 30, height: 30))
but.tag = k
but.title = ""
but.action = Selector("buttonPressed:")
but.target = self
but.bezelStyle = NSBezelStyle(rawValue: 6)!
ar.append(but)
self.view.addSubview(but)
x += 30
k++
}
y += 30
x = 0
}
Run Code Online (Sandbox Code Playgroud)
我需要添加NSClickGestureRecognizer每个以识别鼠标左键单击次数.有没有办法以编程方式做到这一点?
emr*_*s57 10
这应该工作:
let g = NSClickGestureRecognizer()
g.target = self
g.buttonMask = 0x2 // right button
g.numberOfClicksRequired = 1
g.action = Selector("buttonGestured:")
but.addGestureRecognizer(g)
Run Code Online (Sandbox Code Playgroud)
然后
func buttonGestured(g:NSGestureRecognizer) {
debugPrintln(g)
debugPrintln(g.view)
if let v = g.view as? NSButton {
debugPrintln("tag: \(v.tag)")
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2572 次 |
| 最近记录: |