Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

neil.b

macrumors member
Original poster
Nov 20, 2008
65
0
I've been creating my own buttons by subclassing UIButton and drawing the buttons programatically. In order to provide touch functions I've been using the addTarget:action:forControlEvent method but I've come unstuck.

Even though I can use the addTarget method to capture touch events, only the sender gets sent to the selector and so no touch information is available (locationInView etc.)

For example, creating the button;

Code:
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(100,100,50,50)];
[myButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:myButton];

And the selector;

Code:
-(void) buttonPressed:(id)sender
{
        ....
}

As far as I can figure out, there's no way to prototype the buttonPressed selector so that you can pass it touch information in the same way that;

Code:
-(void) touchBegain:(NSSet *)touches withEvent:(UIEvent *)sender
does.

Can anyone give me some pointers?

Thanks
 

drivefast

macrumors regular
Mar 13, 2008
128
0
there's a lot of control events in the same enum where UIControlEventTouchDown is. but if none of them satisfies you, why dont you give up subclassing the UIButton, and just go and subclass an UIView containing a picture of your button? it's more work because you would have to keep track of the image to display on each kind of event, but you have more granular access.
 

neil.b

macrumors member
Original poster
Nov 20, 2008
65
0
Thanks.

What I'm playing around with doesn't particularly require the subclass to be of UIButton so UIView would do.

However, I'm still unsure of how to achieve the same result with a UIView.

I've got a matrix of buttons and what I want to trap is if the user presses a button then drags their finger outside of the bounds of the button, but with more accuracy than the UIControlEventTouchDragEnter/Exit events give. At that point I want to detect if the users finger has moved into the bounds of another button, therefore I need coordinates within the view.
 

drivefast

macrumors regular
Mar 13, 2008
128
0
the way you describe it, sounds like you're still ok subclassing the UIButton, yet you may want to focus on using it as a UIView for what you need. did you had a chance to look at the MoveMe sample app from apple? also the Touches sample may help too.
 

neil.b

macrumors member
Original poster
Nov 20, 2008
65
0
Somehow I've never noticed that Touches example before. That's given me some food for thought.

Thanks :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.