views:

143

answers:

1

I am trying to make an if statement which will check whether the users touch is within a UIButton's bounds. I thought this would be an easy affair as UIButton is a subclass of UIView, however my code doesn't seem to work. This is the code I have been using.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSArray *array = [touches allObjects];
    UITouch *specificTouch = [array objectAtIndex:0];
    currentTouch = [specificTouch locationInView:self.view];
    if (CGRectContainsPoint(but.bounds, currentTouch)) {
        //Do something is in bounds.
    }
    //Else do nothing.
}
+1  A: 

It's not clear what this is a method on, but self is not clear from your code. You might want to do:

currentTouch = [specificTouch locationInView: but];
Ben Gottlieb
That doesn't seem to make any difference. Just to clear things up, 'but' is an IBOutlet to the button and 'currentTouch' is an instance variable to store the current touch.
Joshua
What is this method attached to? Do you know if it's being called at all?
Ben Gottlieb
I know it's being called because it is in my view controller for the view where the user touches. And I have double checked with NSLog.
Joshua
If it's your viewController, than locationInView: self.view will give you basically the coordinates on-screen (minus navigation bar height), whereas you're checking your button's bounds. Assuming but is a child of .view, you might want to try but.frame, not but.bounds.
Ben Gottlieb
It works in part, you see what I am trying to do is move a button where the user is touching, but it seems to suddenly stop. Here's what I mean: http://fwdr.org/file:g4eu .And to help here is my full code: http://gist.github.com/370346 And what the sub-view looks like in IB: http://cl.ly/Vcb. Thanks in advance.
Joshua
I can see a few areas of improvement, but the most important would be to do your CGRectContainsPoint() check in touchesBegan, not touchesMoved. Also, a better way to do it might be to use -hitTest:withEvent: to figure out what view was hit, and if it's your button, do your thing. Once the user has begun dragging the button, it should keep doing so until touchesEnded. All that said, the entire behavior of dragging a button is pretty bad, and will confuse users.
Ben Gottlieb
Thanks very much for the help, by doing the CGRectContainsPoint() check in touchesBegan and setting a Boolean value for whether it is or isn't in the frame and then checking for that in the touchesMoved method fixed the problem. I was using the button just as an example/test. Thanks again.
Joshua
There's just one other thing, to make this work I've had to have my button in it's own view in a separate NIB file, the problem with this is that when the view is added as a subview it blocks out the add button. But if I delete the view the button is in, the button will no longer move.
Joshua
At this point I'd have to see code; there are a lot of moving parts here.
Ben Gottlieb
Sure, here's the source code http://cl.ly/Wa9. I've been able to get the add button working again but I can only move the one button; the first one that was added. Thanks for all your help.
Joshua
This is the wrong way to do what you want here. You KIND of want to do what Bill is suggesting here: http://bill.dudney.net/roller/objc/entry/uitableview_from_a_nib_file. I assume you eventually want to do more than add a simple button, but you should consider constructing your view in code, rather than IB.
Ben Gottlieb
I've taken your advice on constructing and adding the view in code, rather than IB. This is the code I'm using now: http://gist.github.com/372120. There is however (yet again) a problem; only the button that has just been added will move.
Joshua