鼠标事件处理程序 [英] Mouse events handlers

查看:214
本文介绍了鼠标事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MyNSImageView是NSImageView的子类,在这里我有:

MyNSImageView is a subclass of NSImageView, here I have:

@interface MyNSImageView : NSImageView 
{
}
@end

@implementation MyNSImageView

//- (void) mouseDown: (NSEvent *) theEvent 
//{
//  do not wish to implement mouseDown event handler from here
//}
@end

在另一个名为MainView的类中,我有:

In another class called MainView, I have:

@interface MainView : NSView 
{
    MyNSImageView *ImageView1;
    MyNSImageView *ImageView2;

}
@end

- (void)awakeFromNib
{
    ImageView1 = [[[MyNSImageView alloc] initWithFrame:NSMakeRect(5, 5, 240, 240)] autorelease];
    NSImage* image1 = [[[NSImage alloc] initWithContentsOfFile: @"/Volumes/MAC DAT2/pictures/MP6107.jpg"] autorelease];
    [ImageView1 setImage:image1];
    [self addSubview:ImageView1];

    ImageView2 = [[[MyNSImageView alloc] initWithFrame:NSMakeRect(300, 5, 240, 240)] autorelease];
    image1 = [[[NSImage alloc] initWithContentsOfFile: @"/Volumes/MAC DAT2/pictures/MP5784.jpg"] autorelease];
    [ImageView2 setImage:image1];
    [self addSubview:ImageView2];
}

- (void) mouseDown2: (NSEvent *) theEvent 
{
    NSLog(@"mousedown2 from MainView");
}
- (void) mouseDown1: (NSEvent *) theEvent 
{
    NSLog(@"mousedown1 from MainView");
}
@end

- (void) mouseDown: (NSEvent *) theEvent 
{
    NSLog(@"mousedown from MainView");
}

在MainView中,当我点击ImageView1或ImageView2时,我想要使mouseDown1或mouseDown2方法相应地处理事件而不是mouseDown方法。

In the MainView, when I click on the ImageView1 or ImageView2, I would like to have the mouseDown1 or mouseDown2 method to handle the event accordingly not the mouseDown method.

我已经阅读了关于目标/动作/委托和回复者的内容,但仍然看不到要做的这些确切的语法。

I have read about target/action/delegate and responder stuff, but still could not see the exact syntax to do this.

推荐答案

一种处理方式是使用一个委托:

One way to handle this is with a delegate:

首先你为你的 NSImageView 子类:

@class MyNSImageView;
@protocol MyNSImageViewDelegate <NSObject>

- (void)myImageView:(MyNSImageView *)view mouseDown:(NSEvent *)event;

@end


@interface MyNSImageView : NSImageView {

}
// declare the delegate member
@property (assign) id<MyNSImageViewDelegate> delegate;
@end

@implementation MyNSImageView
@synthesize delegate = _delegate;

// In your mouseDown method, notify the delegate
- (void)mouseDown:(NSEvent *)event {
    if([self.delegate respondsToSelector:@selector(myImageView:mouseDown:)]) {
        [self.delegate myImageView:self mouseDown:event];
    }
}

@end

然后,声明您的 MainView 类来实现MyNSImageViewDelegate协议:

Then, declare your MainView class to implement the MyNSImageViewDelegate protocol:

@interface MainView : NSView <MyNSImageViewDelegate> {
    MyNSImageView *imageView1;
    MyNSImageView *imageView2;
}
@end

在您的 MainView 实现:

- (void)awakeFromNib {
    // create your image views then add our instance as the delegate to each
    ImageView1.delegate = self;
    ImageView2.delegate = self;
}

// here we implement the `MyNSImageViewDelegate` method, which will get 
// called when any `MyImageNSView` instance we've added ourselves as
// delegate to gets clicked.
- (void)myImageView:(MyNSImageView *)view mouseDown:(NSEvent *)event {
    if (view == imageView1) {
        NSLog(@"imageView1 clicked");    
    } else if (view == imageView2) {
        NSLog(@"imageView2 clicked");    
    }
}

这篇关于鼠标事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆