ajay
ajay

Reputation: 3345

How to cancel touchesMoved:withEvent:

I have a query regarding touches on the view:

I have a parentView(frame is (0,0,768,1024)) which contains two subViews(A and B),A frame (0,0,512,768) with tag 100 and B Frame (512,0,512,768) with tag 200.

I implemented touches methods touchesBegan:withEvent: touchesMoved:withEvent: touchesEnded:withEvent:.

When I started touch from A to B touchesBegan and touchesMoved methods gets fired as expected

I need to implement: As the touches reaches B from A, touchesEnded of A should get fired and touches began of B be fired and vice versa .I have my own logic in their respective methods.Can some one suggest how to achieve the same.

Upvotes: 0

Views: 529

Answers (2)

Cowirrie
Cowirrie

Reputation: 7226

This is intentional behavior. Usually, if you touch the Cancel button, and your finger slips onto the Delete button as you lift it, you don't want to accidentally delete something.

However, I've had the situation where I did want touches to be passed around like this. Much like @ejazz, I moved the work to the view that contained both A and B. This had user interaction enabled, and implemented touchesBegan, touchesMoved and touchesEnded.

When touches moved over the subviews, it sent appropriate messages. I just switched the subviews on and off, but you should be able to just call touchesBegan, touchesMoved and touchesEnded on the appropriate subview if necessary. If you need to know when touches begin and end, you will need to track the previous view associated with each touch - or just call its previousLocationInView: method.

Upvotes: 1

ejazz
ejazz

Reputation: 2508

I would suggest to use the UIPanGestureRecognizer class. Attach it to the parent view, set the delegate.

You can now follow the states and location. So when the state is UIGestureRecognizerStateBegan check if the location is in A or B, and when the state is UIGestureRecognizerStateEnded check if the location is in B or A.

You can check if the tap view contains the A or B view with

CGRectContainsRect(<#CGRect rect1#>, <#CGRect rect2#>)

And then using view.frame

Upvotes: 0

Related Questions