编辑:第一部分已经解决
很抱歉可能是noob问题,但我搜索了两天没有找到答案。 我已经阅读了有关事件处理的Objective-C文档,但我真的无法将其转换为Rubymotion。
我只是想在NSView上定义一个mouseDown事件,该事件包含带有图像的子视图。
任何提示? 感谢。
新问题
示例代码: 更新了新问题(查看评论)
class ViewController < NSView
def loadWindow
@window = NSWindow.alloc.initWithContentRect([[400, 500], [480, 200]],
styleMask: NSTitledWindowMask|NSClosableWindowMask,
backing: NSBackingStoreBuffered,
defer: false)
@window.setTitle("Test")
@cView = ViewController.alloc.initWithFrame([[400,500], [480, 200]])
@window.setContentView(@cView)
@iView = NSImageView.alloc.initWithFrame([[100,100], [30, 30]])
@iView.setImage(NSImage.imageNamed "Icon")
@cView.addSubview(@iView)
@window.orderFrontRegardless
@window.makeKeyWindow
@var = "variable"
puts @var # This works and puts "variable"
end
def mouseDown(event)
puts "mouse click" # This puts "mouse click"
puts @var # This puts a blank line
end
end
答案 0 :(得分:0)
您的回答是在文档中,但您错过了它:)
处理鼠标按下事件由NSView的父类NSResponder处理: https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/NSResponder/mouseDown:
您只需要在视图上定义mouseDown。
class MyView < NSView
def mouseDown(event)
# what you want to do
end
end
答案 1 :(得分:0)
如果使用sugarcube gem,你可以这样实现:
button = UIButton.alloc.initWithFrame([0, 0, 10, 10])
button.on(:touch) { my_code }
button.on(:touch_up_outside, :touch_cancel) { |event|
puts event.inspect
# my_code...
}
# remove handlers
button.off(:touch, :touch_up_outside, :touch_cancel)
button.off(:all)