views:

83

answers:

2

Hello all,

assume that a five buttons side by side, i need to know how to make them accept (Called) the drag from outside to inside the border of each button

i.e. Like the piano if u drag your finger every button called and make sound

Thanks

+1  A: 

You should add a handler for the 'drag enter' event for each button - I'm not sure that you can do this in interface builder but here's how to do it in code.

Put this in your viewDidLoad method :

[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];

and then

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

Have a look at this for the different types of control events you can listen for.

NB This example assumes that you have got this in your .h file :

@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;

and connected them correctly in interface builder etc.

deanWombourne
Hello, first thanks for the answer second I did what u had written but i got 2 warning and the Viewdidload stop maybe i forgot somthing can u help me more plz
Bobj-C
I can only help if you tell me what the warnings were ;)
deanWombourne
ok in the void method warning passing arrgument 1 of "NSLog" from incompatible pointer type second warning: 'AVAudioPlayer' may not respond to '-addTarget:action:forControlEvents:' i did what u told me plus in the .h file i wrote the signature of ur void method
Bobj-C
The first warning is easy, its just a typo! I missed out a '@' - I've edited the question to fix it.
deanWombourne
The second error is more complicated - it looks like button1 is an AVAudioPlayer, not a UIButton as your question implied. My code will only work on objects that inherit from UIControl, like UIbuttons etc. An AVAudioPlayer isn't a view so I don't see how you can be putting it onto the screen and expecting it to be touched?
deanWombourne
Now all work properly but the simulator stop and show me a blak screen if i comment the [button1 addTarget:self action:@selector(buttonEntered:) forControlEvents:UIControlEventTouchDragEnter]; the application work but why this is really wired
Bobj-C
It's not weird at all - you're calling a method on an object that doesn't have that method so it crashes. What is button1? If you're using interface builder you should have a line like `@property (nonatomic, retain) IBOutlet UIButton *button1;` somewhere in your .h file?
deanWombourne
ok ok thank u, your is code great i solve it, it is my problem i confused between the button name and the sound name i passed the sound name not the button it seem work but can i test it in the simulator ?
Bobj-C
Ah, that would explain it :) It should work in the simulator or on a phone, I can't think of a reason why it wouldn't. Glad to help.
deanWombourne
ok thank u again i will try in my phone but today i got the approve from Apple to be a developer and till now i doesn't submit my iPhone
Bobj-C
hello again nothing done in the device still the IBAction not called
Bobj-C
A: 

Hello I found the Answer Here:

Question 1

Question 2

Bobj-C