how identify touch in a particular view - iphone

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

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

How to get which object began touch in 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)
{
}
}

Drag images and resize them on touch(iPhone)

I am creating an app in which when user click on any object and when user tap on UIView, then that images should drop on the view, and on dragging that image in any direction it should be re-sized.
Anyone having any idea to do this, please let me know.
or Can I get any sample code for this?
Thanks
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == photo) {
photo.center = touchLocation;
}
else if ([touch view] == photo1) {
photo1.center = touchLocation;
}
}
or you can see in that link http://www.roseindia.net/tutorial/iphone/examples/iphone-DoubleImageMoveasd.html

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.