简体   繁体   中英

Detect view that finger slid over in iPhone SDK

My problem involves creating a control somewhat analogous to the apple magnifying glass in text view control, where you touch for a second or so to pop up a magnifying glass and then drag your finger around to select your desired location.

I created all the methods that I need for detecting a long stationary touch. Now I need to detect the UIView that my finger has slid over. I'm not exactly sure how to go about making this happen, can anyone point me in the right direction?

Any UIView that needs to report that it was touched can detect the touch using the UIResponder inherited methods

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:

UIViews that you want to be able to report touches can be inherited from, and in your subclass you can define a delegate method to be called from within the methods above. By setting the delegate to some central reporting class you can have all the touches and the views that received them identified.

Don't forget to pass on the touch event, ie:

[[self superview] touchesBegan:touches withEvent:event];

if you want touch events to work normally.

In your method that is handling the touches moved you could send a hit test to the superview that is containing your various subviews. The hit test will return the deepest view at a current point in a view. It might look like this.

//Just detected that the finger is stationary
MyView *touchedView = [containerView hitTest:touchPoint withEvent:event];
[touchedView performSomeAction];

You should probably perform some class checking to make sure that the view is actually a view that can react to the touch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM