2013-12-13 79 views
0

编辑:第一部分已经解决Rubymotion:我如何为NSView定义一个mouseDown事件?

对不起,我可能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

你的答案是在文档,但你只是错过了: )

处理鼠标按下事件由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 
+0

我已经尝试过这种方式,但它不工作。也许我错过了别的东西。 – vash

+0

我更新了第一篇文章 – vash

+0

@vash,你是否正确地继承了你的视图,因为这是正确的方式来做到这一点。你应该检查你的代码,因为你有一个ViewController类继承自一个NSView ... – siekfried

0

如果使用sugarcube宝石,你可以这样实现它:

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) 

细节:https://github.com/rubymotion/sugarcube

+0

Sugarxube在osx上不可用。 – siekfried

+0

@siekfried现在支持ios和osx。 – anguskwan

相关问题