How can I move multiple objects with Objective-C - ios

Here is my code
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [[event allTouches] anyObject];
UITouch *myTouch2 = [[event allTouches] anyObject];
UITouch *myTouch3 = [[event allTouches] anyObject];
button.center = [myTouch locationInView:self.view];
button2.center = [myTouch2 locationInView:self.view];
button3.center = [myTouch3 locationInView:self.view];
}
The problem is, when I try to move one of the buttons, they all move at the same time and same position. I want to be able to move buttons separately and freely. How do I do that? Any suggestions?

Use the following to determine which button it is:
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint location = [touch locationInView:touch.view];
if(CGRectContainsPoint(button.frame, location))
{
button.center = [myTouch locationInView:self.view];
}
else if(CGRectContainsPoint(button2.frame, location))
{
button2.center = [myTouch locationInView:self.view];
}
else if(CGRectContainsPoint(button3.frame, location))
{
button3.center = [myTouch locationInView:self.view];
}

I want to be able to move buttons separately and freely.
Then you shouldn't move them all to the same place at the same time:
UITouch *myTouch = [[event allTouches] anyObject];
UITouch *myTouch2 = [[event allTouches] anyObject];
UITouch *myTouch3 = [[event allTouches] anyObject];
Take a look at myTouch, myTouch2, and myTouch3 -- they may all point to the very same touch object. And in that case, you'll be moving all your buttons to the same location.
If you're expecting multiple touches, you'll need to look at the set of touches in [event allTouches] and make sure that you're getting distinct objects for each button. You could for example pick the touch object that's closest to each button, making sure that you don't use the same touch for two buttons.

Related

Moving button like assistive touch

Hi I want to move a uibutton where even I want in my app like we have Assistive touch in iphone so as a same I want that kind of button in my app..Is it possible to do that If so please help out with code are some links for that
This question is asked so many times.... here is answer
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
button.frame.x = touchlocation.x;
button.frame.y = touchlocation.y;
}

ios locationInView not show correct log when I click direct view

I feel strange, I have dark view(darkScreenView) and green view.
I using the
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchViewPoint = [touch locationInView:touch.view];
NSLog(#"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkView pointInside:touchViewPoint withEvent:event]);
}
When I click on the dark view or green view,
The Log still show 1. why?
I click on the green view should show log 0, but I still get the log result 1.
I am using xcode 6.3.1.
Have anyone know what problem in this situation?
Thank you~
This will work
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchViewPoint = [touch locationInView:touch.view];
CGPoint pointOfDarkView = [touch.view convertPoint:touchViewPoint toView:self.darkview];
NSLog(#"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkview pointInside:pointOfDarkView withEvent:event]);
Your problem:
touchViewPoint is relative to touch.view coordinate
You have to convert point to darkview coordinate first,then you can use pointInside:pointOfDarkView
Try to get touch for different views..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if (touch.view==GREEN_VIEW)
{
//Write your code for green view here
}
else if(touch.view==DARK_VIEW)
{
//Write your code for dark view here
}
}
Your touchViewPoint's location is relative to the touched view. So get location relative to self.darkView like below.
CGPoint touchViewPoint = [touch locationInView: self.darkView];
Here is the problem change to View you are referring to.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchViewPoint = [touch locationInView:darkView]; <----- Problem
NSLog(#"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkView pointInside:touchViewPoint withEvent:event]);
}

Obtaining touch location inside a TouchDragInside method

How do I obtain the touch location inside a TouchDragInside method when there is no event parameter passed to the method?
Obviously this does not work:
- (IBAction)touchDragInsideDblTapSignBut:(id)sender {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
}
Thanks,
So in my viewDidLoad I had to add this:
[_dblTapSignAddBut addTarget:self action:#selector(touchDragInsideDblTapSignButE:event:) forControlEvents:UIControlEventTouchDragInside];
And then create a void method:
- (void) touchDragInsideDblTapSignButE:(id)sender event:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
NSLog(#"Location x%f y%f",location.x,location.y);
}

Two player pong-like game on one phone

I am making a pong-like game for one iPhone where two players control a paddle from each end of the phone. I'm having trouble moving paddles independently. I am getting them both to move exactly the same with the following code:
UITouch *touch1 = [[event allTouches] anyObject];
CGPoint location = [touch1 locationInView:touch1.view];
CGPoint yLocation = CGPointMake(p1_paddle.center.x,location.y);
p1_paddle.center = yLocation;
UITouch *touch2 = [[event allTouches] anyObject];
CGPoint location2 = [touch2 locationInView:touch2.view];
CGPoint yLocation2 = CGPointMake(p2_paddle.center.x,location2.y);
p2_paddle.center = yLocation2;
I have done some researched and learned that this may be possible by splitting the view into two different segments, one for each player. This is the code I used, but it isn't working, it moves both paddles to one side of the screen up in a corner, not even moving:
UITouch *touch1 = [[event touchesForView: p1_field] anyObject];
CGPoint location = [touch1 locationInView:touch1.view];
CGPoint yLocation = CGPointMake(p1_paddle.center.x,location.y);
p1_paddle.center = yLocation;
UITouch *touch2 = [[event touchesForView: p2_field] anyObject];
CGPoint location2 = [touch2 locationInView:touch2.view];
CGPoint yLocation2 = CGPointMake(p2_paddle.center.x,location2.y);
p2_paddle.center = yLocation2;
Unless I'm missing some simple logic in the view part, I'm lost. Any suggestions?
OK, after doing some more research and toying around, I finally got it working. Below is the code. This is with the phone in landscape orientation.
for (UITouch *touch in [event allTouches]) {
CGPoint location = [touch locationInView:touch.view];
if (location.x < 240) {
CGPoint yLocation = CGPointMake(p1_paddle.center.x,location.y);
p1_paddle.center = yLocation;
}
if (location.x > 240) {
CGPoint yLocation2 = CGPointMake(p2_paddle.center.x,location.y);
p2_paddle.center = yLocation2;
}
}
ALSO REMEMBER in your viewDidLoad method,
self.view.multipleTouchEnabled = YES;

Moving a sprite by touching anywhere on the screen

In my game only one sprite responds to touches. How can I make a mousejoint move this sprite/body regardless of where I touch on the screen?
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
mySprite.position = location;
}
Make sure in your initmethod that self.isTouchEnabled = YES;

Resources