how to make multi touch enabled ios app - iphone

I have made an app and i want to make it multi touch compatible. i have tried looking around but the answers aren't specific to mine.
Here is what i do:
1) My coding:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesMoved:touches withEvent:event];
if (gameState == kGameStatePaused) {(gameState = kGameStateRunning);}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(gameState == kGameStateRunning) {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if(location.x > 400) {
CGPoint yLocation = CGPointMake(playerPaddle.center.x, location.y);
playerPaddle.center = yLocation;
}
if (gameStyle == kGameStyleTwoP) {
if(location.x < 100) {
CGPoint yLocation2 = CGPointMake(computerPaddle.center.x, location.y);
computerPaddle.center = yLocation2;
}
}
}
2) I have gone onto Interface Builder and checked the box for multitouch enabled
3) I build and run my app and it opens properly and when i go to test the multitouch i hold "option key" and click and move the mouse
4) (i am trying to make computerPaddle and playerPaddle both move) but only one of the works at a time
I have to tried to fix it but i can't understand where i am going wrong.
Any help is useful.
THX.

There is a property named multipleTouchEnabled on the UIView that you can set to YES to enable it (defaults NO).
Also, you should loop to treat all touches in the touches set that you receive in touchesMoved.

look this line
UITouch *touch = [[event allTouches] anyObject];
you only take one touch and ignore rest and thats why only one thing can move
so replace with a for loop should fix the problem
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(gameState == kGameStateRunning) {
for (UITouch *touch in [event allTouches]) {
CGPoint location = [touch locationInView:touch.view];
if(location.x > 400) {
CGPoint yLocation = CGPointMake(playerPaddle.center.x, location.y);
playerPaddle.center = yLocation;
}
if (gameStyle == kGameStyleTwoP) {
if(location.x < 100) {
CGPoint yLocation2 = CGPointMake(computerPaddle.center.x, location.y);
computerPaddle.center = yLocation2;
}
}
}
}

Related

How can you detect if SKShapeNode is touched?

I created a sknodeshape. How can I detect if the shape I made was touched(clicked)?
Here is the code: (I solved it already)
//metung_babi is the name of the SKShapeNode
UITouch *touch = [touches anyObject];
CGPoint nokarin = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:nokarin];
if ([node.name isEqualToString:#"metung_babi"]) {
NSlog(#"touch me not");
}
my mistake was when I created the shape I put the SKShapeNode name before initializing it.
Implement the touch delegate methods. Then, in the -touchesBegan: method, extract the point of touch and retrieve the node using the [self nodeAtPoint:] method
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *yourNode = ....;
CGRect yourNodeFrame = bowlNode.frame;
if (CGRectContainsPoint(yourNodeFrame, location)) {
//your node may be touched, check if it's your node
SKNode *theNode = [self nodeAtPoint:location];
if ([theNode.name isEqualTo:yourNode.name]) {
//it's your node touched
}
}
}
the method nodeAtPoint return value is complicated. you can check the document to find different situations

Continuous Touch detection in imageView using Touch event..?

I have a method which call the another view. i want to call this method after the image is continuously for a definite time.
This type of method found in UIButton = touchDown(),touchup().
Is there any method in touch event to find continuous touch in image view.
Pls help me.
thanks in advance
You can use these events for UIIMAGEVIEW:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
You can use the tap gesture which you can see the answer i wrote in this thread.In this you can set the number of taps which needs to fire the event.Hope this helps you.
assuming you want to count the touch (continous like double tap)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
int index =[touch view].tag;
if (touch.tapCount == number && index == imageTag) {
}
}
tapCount will be the count of tap in continues time with a very short time interval (double tap). you cannot use it if the count of tap you want to watch has longer delay (say 5 single taps). alternatively, you can remember the touches for your imageview, something like:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
int index =[touch view].tag;
if(index == imagetag){
if([tempMutableArray count] < definiteTime){
[tempMutableArray addObject:#"any"]
}else{
[tempMutablArray removeAllObjects];
//you can call the method now
}
}
}

How can we check that which image in imageview is being called

I am having 2 imageviews with image in it. I want that when I click on the first image the image should get selected and if it is selected it should return me value TRUE or 1 that should be saved in sqlite database. How is this possible?
By using UITouch class methods you will get which image view is touched, or you can put imageview inside button then you will get click event.
You go with Touch events.Capture the touch points and perform your actions.Here I will give u a sample structure to do this,
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location= [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location)) {
//set some flag like
selectionFlag=1; }
else if(CGRectContainsPoint(secImage.frame, location)){
selectionFlag=2; }
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location)) {
if(selectionflag==1) {
//do ur db actions }
}
else if(CGRectContainsPoint(secImage.frame, location)) {
if(selectionflag==2) {
//do ur db actions }
}
selectionflag=0;
}
first do
[self.*yourimageViewname* setUserEnteractionEnabled:YES];
BOOL select1,secelect2;
select1=NO;
select2=NO;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
//give the beginning and ending x and y points in condition to check which imageView is taped
if(touchLocation.x>1 && touchLocation.x<116 && touchLocation.y>133 && touchLocation.y<233)
{
select1=YES;
}
else if(touchLocation.x>120 && touchLocation.x<300 && touchLocation.y>133 && touchLocation.y<233)
{
select2=YES;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
On the condition which boolean variable is true you can save or whatever you want to do further coding your application.
Using the touches is one way as explained in the answers below. If you have just two imageview you can also try to have two custom transparent buttons on top of that imageview and therefore you can easily know which image is touched based on the tag you give for the buttons.
What you could also do is create two buttons (instead of UIImageViews) that have images. They should display roughly the same (you could even disable the touch states, etc). And you get UIResponder events for free (as in; you can target the action to a selector).
UPDATE: here's roughly how (didn't bother configuring the buttons though).
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:yourImage forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
// .. now create a second button .. //
And your button will go to the following method when touched:
- (void)buttonTouched:(id)sender
{
// .. add your stuff to your database .. //
// .. you can identify your button by sender, or give the button a tag (number) to identify it .. /
}

a question about multitouch

I have several uiimages that I can move around on the iphonescreen using multitouches. The thing is that I want to separate them in two "teams" , a "team" of uiimages that I move inside an area of my choice and a "team" that I can move all over the screen.
My question is how to use the touch methods (touchesbegan, touchesended, touchesmoved) for both of the two uiimage "teams" and their cgpoints without the cgpoints from both "teams" crossing each other and giving wrong uiimages wrong positions on the screen. the 1st "team" uses the touchesbegan, touchesmoved and touchesended methods. the 2nd "team" only uses the touchesended method.
Here´s my code. I hope that the 2 "teams" don´t cross with each other in the touchesended method
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//1st team
for(UITouch *touch in touches){
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self getRubyAtPoint:[touch locationInView:self.view]forEvent: nil];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"touchesEnded");
//1st team
// Enumerates through all touch object
for (UITouch *touch in touches) {
// Sends to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:self.view]];
}
//2nd team
UITouch *touch = [touches anyObject];
CGPoint currentTouch = [touch locationInView:self.view];
[self getPieceAtPoint:currentTouch];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//1st team
NSLog(#"touchesMoved");
// Enumerates through all touch objects
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchTouchEvent:[touch view] toPosition:[touch locationInView:self.view]];
}
}
If I understand, you are asking how to keep the methods such as getPieceAtPoint from acting on the wrong team.
I think I'd just add a unique tag range to each team and check that before acting on it.
Something like:
UITouch *touch = [touches anyObject];
if ([touch view].tag>=TEAM_TWO_BASE_TAG)
{
CGPoint currentTouch = [touch locationInView:self.view];
[self getPieceAtPoint:currentTouch];
}

Handling touches on Iphone

Im having a little problem on handling touches in my apps.
I set my touchesBegan like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *currentTouch = [[event allTouches] anyObject];
touchPoint = [currentTouch locationInView:self.view];
if (CGRectContainsPoint(image1.frame, touchPoint)) {
image1IsTouched = YES;
}
}
Then i set my touch move like this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *currentTouch = [[event allTouches] anyObject];
currentPoint = [currentTouch locationInView:currentTouch.view];
if(image1IsTouched == YES) {
image1.center = CGPointMake(currentPoint.x,currentPoint.y);
.....
}
}
Now i tried my app on actual unit and thats where i notice my problem. While im touching the image1 with 1 finger the app is doing ok and its checking for collision everytime i drag my finger. The problem occurs when i touch the screen with another finger while touching/dragging the image. The image im currently touching will jump to the other finger. I've tried [myView setMultipleTouchEnable:NO]; & using NSArray on touches and comparing the [touches count] with the touch but its not working. Can someone show me how to set a uiimageview to act on single touch only. Thanks.
First, you should use UITouch *currentTouch = [touches anyObject]; to get the current touch.
Second, you should check that touches.count == 1 to make sure there's only one finger on the screen, and ignore touch input if there's more than one, unless you wanted to support multitouch.