How to get which object began touch in iphone - iphone

I have one view in iPhone application. In that view i added two UIImageView lets say img_view1, img_view2. Now i put touch event on that view as following.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Some code are here
}
how do i can get which image view began to touch ?

UITouch *touch = [touches anyObject];
CGPoint pt = [touch locationInView:self];
if (CGRectContainsPoint(img_view1.frame, pt)
{
NSLog(#"Image View 1 touched");
}
etc.

Make your image view user interaction enabled in the xib (or through the code if you are adding it programmatically) and use the UIResponder methods touchesBegan, touchesMoved, touchesEnded etc to detect the touch on the image view:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == yourImageView)
{
//add your code for image touch here
}
}

Use this code
UITouch *touch = [touches anyObject];
if (touch.view==imageView1) {
// do something
}
else if (touch.view==imageView2) {
// do something else
}
Hope this is what you are looking for.
Enjoy coding :)

use this code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if(touch.view == img_view1)
{
}
else if(touch.view == img_view2)
{
}
}

Related

Multi object drag

While dragin multi objects using the following code the the object is shaking when i touch the object,
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == img1)
{
CGPoint location = [touch locationInView:touch.view];
img1.center = location;
return;
}
if (touch.view == img2)
{
CGPoint location = [touch locationInView:touch.view];
img2.center = location;
return;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
What is problem in my code, is there any other method to drag and if the view contain one more object?please help me to solve this issue
I think if you are talking about more object to drag all at once then you should remove the return from you touch begain method

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

Disable UIView on TouchesMoved

I have an application in which i have to move images, problem is arising when mouse touches the images it gets moved but if it touches the main View, whole view is also moving.
I tried view.userInteractionEnabled=NO;
but after one move whole view gets freeze.
I want my view to be static(not moving)
Help !!!
here is the code
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
UIImageView *imgPimples = [[UIImageView alloc]init];
imgPimples = (UIImageView*)[touch view];
CGPoint touchLocation = [touch locationInView:imgPimples];
imgPimples.center = touchLocation;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
Just check whether touched class is of kind UIImageView.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([[touch view] isKindOfClass:[UIImageView class]]) {
UIImageView *imgPimple;
imgPimple = (UIImageView *)[touch view];
CGPoint touchLocation = [touch locationInView:imgPimple];
imgPimple.center = touchLocation;
}
}
I think it will serve your purpose.
if ([touch.view isKindOfClass:[UIImageView class]]){
imgPimples.center = touchLocation;
}
assuming your parent view is not also a UIImageView.

UIScrollview touchesbegan,touchesmoved,touchesended actions

I did this touch actions in UIView, ie,
in a uiview there are two or three subviews v1,v2,v3. I used the code below to place the image i1,i2,i3 in corresponding views and if I moves the touch the images move to that point in the view.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == v1)
{
CGPoint location = [touch locationInView:v1];
i1.center = location;
}
else if([touch view] == v2)
{
CGPoint location = [touch locationInView:v2];
i2.center = location;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == v1)
{
CGPoint location = [touch locationInView:v1];
i1.center = location;
}
else if([touch view] == v2)
{
CGPoint location = [touch locationInView:v1];
i2.center = location;
}
}
Now I have to do this for a sequence of 28 images so I went for Uiscrollview but I cant get that please explain me clearly with code. thanks a lot in advance for your help.
You need to subclass UIScrollView to get touch events of UIScrollView.

how identify touch in a particular view

How to get touch on a particular view.
I am using
CGPoint Location = [[touches anyObject] locationInView:self.view ];
but want to trigger the action only if an particular subView is clicked.
How to do this.
I got the answer myself...but thanks other which helped me on this
here it is
UITouch *touch ;
touch = [[event allTouches] anyObject];
if ([touch view] == necessarySubView)
{
//Do what ever you want
}
Try this
//here enable the touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(yoursubview_Name.frame, touchLocation)) {
//Your logic
NSLog(#" touched");
}
}
You should create a subclass (or create a category) of UIView and override the
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
where redirect the message to the appropriate delegate.
// here Tiles is a separate class inherited from UIView Class
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([[touch view] isKindOfClass:[Tiles class]]) {
NSLog(#"[touch view].tag = %d", [touch view].tag);
}
}
like this you can find view or subview is touched
Heres a swift3 version without multitouch:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first, self.bounds.contains(touch.location(in: self)) {
// Your code
}
}
Did u try
CGPoint Location = [[touches anyObject] locationInView:necessarySubView ];