手指拖到你的臀部

时间:2010-09-30 11:22:15

标签: iphone objective-c cocoa-touch

假设并排五个按钮,我需要知道如何让它们接受(调用)从外部到每个按钮边框内部的拖动

即。就像钢琴一样,如果你用手指拖动每个按钮并发出声音

由于

2 个答案:

答案 0 :(得分:2)

你应该为每个按钮的'drag enter'事件添加一个处理程序 - 我不确定你是否可以在界面构建器中执行此操作,但这里是如何在代码中执行此操作。

将它放在viewDidLoad方法中:

[button1 addTarget:self action:@selector(buttonEntered:) forControlEvents:UIControlEventTouchDragEnter];
[button2 addTarget:self action:@selector(buttonEntered:) forControlEvents:UIControlEventTouchDragEnter];
[button3 addTarget:self action:@selector(buttonEntered:) forControlEvents:UIControlEventTouchDragEnter];
[button4 addTarget:self action:@selector(buttonEntered:) forControlEvents:UIControlEventTouchDragEnter];
[button5 addTarget:self action:@selector(buttonEntered:) forControlEvents:UIControlEventTouchDragEnter];

然后

- (void)buttonEntered:(UIButton *)button {
    NSLog(@"Dragged into %@", button);
}

为您可以收听的不同类型的控制事件设置look at this

注意这个例子假设你的.h文件中有这个:

@property (nonatomic, retain) IBOutlet UIButton *button1;
@property (nonatomic, retain) IBOutlet UIButton *button2;
@property (nonatomic, retain) IBOutlet UIButton *button3;
@property (nonatomic, retain) IBOutlet UIButton *button4;
@property (nonatomic, retain) IBOutlet UIButton *button5;

并在界面构建器等中正确连接它们。

答案 1 :(得分:0)

您好我在这里找到答案:

Question 1

Question 2