UITouch events calls / works only once. not works second time, - iphone

In my application i have added a google maps on my view.
I want to know the touch point on the mapView / myView, where did i taped
So that i implemented logic as follows.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:mapView_];
CGPoint touchPoint2 = [[touches anyObject] locationInView:self.view];
NSLog(#"========== %#", NSStringFromCGPoint(touchPoint));
NSLog(#"========== %#", NSStringFromCGPoint(touchPoint2));
//UITouch *touch = [[event allTouches] anyObject];
//CGPoint touchPoint = [touch locationInView:touch.view];
}
But it works only for the first touch, from the next touch event this method doesn't calls.
I didn't get what was the problem is

You can't get the touch loctaion on a map,when we touch it ,it becomes zoom,the touch events is on the UIView

with the help of this i get a log at everytime i touch on mapview with exact touch points
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.viewMap];
NSLog(#"==========1 %#", NSStringFromCGPoint(touchPoint));
}

Related

Trying to write a simple app to allow user to drag a image

I'm trying to write an app to allow a person to drag around an image of the earth.
The method touchesmoved gets called when the user begins to drag his feet, but [[ event allTouches] anyObject] never returns the earth object, so it never gets dragged,
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch=[[ event allTouches] anyObject];
if ( [touch view] == earth )
{
CGPoint location = [ touch locationInView: self.view];
earth.center=location;
}
}
for get touch use UITouch *touch=[touches anyObject];
Change your touchesMoved: like:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[ touches anyObject];
if ( [touch view] == earth )
{
CGPoint location = [ touch locationInView: self.view];
earth.center=location;
}
}

Drag image from carousal to imageview

I want to drag image from carousel of image and put it in another imageview.After drag of it should delete from carousel.I have done code for this that will also remove from carousel.
I used touch events.
But problem is that after draging one image when i touch anywhere else in the screen it will select next image automatically.How can i stop it?I want to drag image when i click on it.
My Code.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
btnBackImg.center = location;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint pointMoved = [touch locationInView:self.view];
btnBackImg.frame = CGRectMake(pointMoved.x, pointMoved.y, btnBackImg.frame.size.width,btnBackImg.frame.size.height);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touch end");
if (btnBackImg != nil)
{
UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint(basket_image.frame, [touch locationInView:self.view]))
{
basket_image.image=[UIImage imageNamed:[NSString stringWithFormat:#"wit_bas.png"]];
[self.product_categories removeObjectAtIndex:imgIndex+1];
[carousel reloadData];
}
}
}
In this btnCackImg want to move in to basketImg.
If anyone konw then pls help me or if any link then also useful.
Thanks in Advance.
You have used this code in your touches.began CGPoint location = [touch locationInView:touch.view]; where touch.View means touching all the objects in the view namely self.view. What you really need is to add the name of the image to be put instead of touch.View. This will select the image at one time.
CGPoint location = [touch locationInView:imageName];

Get X and Y coordinate Of view

I have one UITableView in My View.The name of my UITableView is tblTask.
I am using touchesBegan method to get the x and y coordinate of my view.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
NSLog(#"point........ %f .. %f",location.x,location.y);
}
But when I begin the touch event from the tbltask. It does not return the location.x and location.y even if tbltask is the subview of the main view.
I tried to disable the scrollview of the table also but nothing happen.
when I set this.
tbltask.userInteractionEnabled=NO;
Then it will give the coordinate but I want the coordinate of view even if tbltask interaction is enabled.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch ;
touch = [[event allTouches] anyObject];
if ([touch view] == self.view){
CGPoint location = [touch locationInView:touch.view];
NSLog(#"point........ %f .. %f",location.x,location.y);
}
}

UITouch object behaving oddly

I wanted to get points where user is touching on the screen therefore I wrote following code which will fire when user will touch somewhere on the screen
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [[UITouch alloc] init];
touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
if (CGRectContainsPoint([dob frame], point)) {
[self showDatePickerforDOB:YES];
}
}
but this code is giving run time error. Upon debugging it was revealed that locationInView is not recognized as a function of touch object on the other hand it is documented in iphone class reference documentation. When I changed code to exclude alloc i.e
UITouch *touch;
touch = [touches anyObject];
then locationInView is perfectly working fine. Any ideas why UITouch *touch = [[UITouch alloc] init]; is giving runtime error.
I think you should not alloc and init the touch pointer, the correct code should be:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
if (CGRectContainsPoint([dob frame], point)) {
[self showDatePickerforDOB:YES];
}
}
[touches anyObject] will return an autorelease object so you don't need to alloc and init the touch pointer.
I got the reason why there was an error. It was because of line
CGPoint point = [touch locationInView:self];
when I changed it to CGPoint point = [touch locationInView:self.view]; runtime error was removed. The reason is that locationInView function takes UIView as its parameter and on the other hand i was giving it a delegate i.e 'self'.
so self.view solved the issue

get touch coordinates (in the form of 0-480 and 0-320)

i dont get how to translate the coordinates of the iphone.
if i make a single touch i get the coordinate.
if i make a touch and keep it and release it it shows the difference of the starting and end point.
how do i get the absolute position of the touch?
thanks!
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
NSLog(#"X: %f",location.x);
NSLog(#"Y: %f",location.y);
}
I want to resize an image with touch and drag(only the height)
The position of a touch is calculated relative to a view.
If you want the position relative to the screen do:
UITouch * touch = [touches anyObject];
CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
NSLog(#"Position of touch: %.3f, %.3f", pos.x, pos.y);
(dry coded, might give errors)