1

I have a virtual keyboard in my app with 6 keys, and the whole thing is just an image implemented with UIImageView. I determined the exact x and y coordinates that correspond to the image of each 'key' and used the following code to respond to a user interacting with the keyboard:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [[event allTouches] anyObject];
    CGPoint point = [touch locationInView:touch.view];
    if(point.y < 333 && point.y > 166 && point.x < 72 && point.x > 65)
    {
    NSLog(@"Key pressed");
    }
    //Repeat per key...
}

However, I have realized that this method is not very smart, because changing phone perspectives (portrait to landscape) or changing devices will ruin my x and y coordinates and therefore cause problems.

So, I am looking for an alternative to specifying the absolute x and y values, and using touchesMoved in general. Ideally, it would be a button with specific settings that would call its method if it was tapped, or if the user dragged their finger into the area of the button (even if very slowly - I used swipe detection before and it required too much of an exaggerated movement).

Is it possible to set up a button to call its method if tapped or if a touch event started outside of the button and then proceded into the button? If not, what are my alternatives?

Thanks SE!

jake9115
  • 3,964
  • 12
  • 49
  • 78

1 Answers1

0

You need to get the winSize property, which will fix the problem you are having with screen sizes.

CGSize size = [[CCDirector sharedDirector]winSize];

I do believe you are using Cocos2D? If so you can use this size property instead of hard coding numbers. :)

to convert your point, use

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];

To see if its within the bounds/box of the button you could try:

if (CGRectContainsPoint ([self.myButton boundingBox], location)
{
//Execute method 
}

This is all assuming you are using Cocos2D and a CCSprite for your button. This should work on any screen size and portrait or landscape :)

LAMBORGHINI
  • 338
  • 2
  • 13
  • Thanks for the answer @CodeBandits, but if possible I want to nix the whole current method I have going and setup a specialized button to reposond similarly. Obviously a tap event within a button's area will trigger the method, but how could I add a trigger for when a user's touch is dragged into the button area? – jake9115 Jun 13 '13 at 01:17
  • I googled what you want, and I saw that you have asked this sort of question in the past [here]. Was there a problem with using this method? (http://stackoverflow.com/questions/16661496/using-uicontroleventtouchdragenter-to-trigger-a-buttons-method-but-doesnt-w) – LAMBORGHINI Jun 13 '13 at 01:21
  • It worked well for a very specific purpose, but overall wasn't a friendly way to portably use the same code elsewhere. If I could find a way to use only UIButton properties, it would be way more helpful! I posted my specifications here: http://stackoverflow.com/questions/17077889/triggering-a-uibuttons-method-when-user-drags-finger-into-buttons-area – jake9115 Jun 13 '13 at 01:26