Obtaining touch location inside a TouchDragInside method - ios

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);
}

Related

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]);
}

How can I move multiple objects with Objective-C

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.

I'm having problems moving an programatically added UIIMAGEVIEW

So , i am having a problem moving some images with touchesBegan and touchesMoved here is my code for the p1 UIImageView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: touch.view];
NSLog(#"incepe");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (touch.view == self.p1) {
CGPoint location = [touch locationInView: self.view];
self.p1.center = location;
NSLog(#"nmk");
}
}
and the way i add the pictures is this
-(void)img{
if (!self.p1) {
self.p1 =[[UIImageView alloc] initWithFrame:CGRectMake(14,20,70,50)];
}
[self.view addSubview:self.p1];
}
the touchesmoved event is not triggred when i press the p1 UIImageview
try setting p1.userInteractionEnabled = YES
Have a I look at this:
-(void)viewDidAppear:(BOOL)animated{
UIPanGestureRecognizer *pgr1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(image1Moved:)];
[yourImageView addGestureRecognizer:pgr1];
}
-(void)image1Moved:(UIPanGestureRecognizer *)pgr{
NSLog(#"Came here");
[yourImageView setCenter:[pgr locationInView:self.view]];
}
P.S yourImageView should have userInteractionEnabled to YES

how to move separately two UIImageView

I have two UIImageView in the same view,I would like to move them but separately.When I move the first one,the second one should stay fix and conversely.
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:[self view]];
[imageView1 setCenter:location];
[imageView2 setCenter:location];
}
this method is a method from UIResponder class will and will be called when the UIView (UIImageView is a subclass so no problem) is moved.
What you have to do, is subclass UIImageView and don't implement this method in your superview.
MovableImageView.h
#interface MovableImageView : UIImageView {
}
MovableImageView.m
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:[self view]];
[self setCenter:location];
}
Each one of your two UIImageView should now be MovableImageView

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