Detection of [UITouch view] in touchsMoved method

I want to drag from one subtitle to another in my application (and connect them to the line), and therefore I need to make sure that the current view is affected by the user.

I thought I could achieve this by simply calling [UITouch view] in the touchhesMoved method, but after quickly looking at the documents, I found that [viewing UITouch] only returns the view in which the initial touch occurred.

Does anyone know how I can detect that the view is touched while dragging and dropping?

+3
source share
3 answers

After several studies, I found the answer.

Initially, I looked at the view like this:

if([[touch view] isKindOfClass:[MyView* class]])
{
   //hurray.
}

, , [touch view] , . , :

if([[self hitTest:[touch locationInView:self] withEvent:event] isKindOfClass:[MyView class]])
{
    //hurrraaay! :)
}

,

+2

:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([self pointInside:[touch locationInView:self] withEvent:event]) {
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
    } else {
        [self sendActionsForControlEvents:UIControlEventTouchUpOutside];
    }
}
+2

UIView is a subclass of UIResponder. That way, you can override the end end / started methods in your custom class, which inherits from UIView. Then you must add a delegate to this class and define a protocol for it. In methods

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

or any interactions you need, just send the appropriate message to the delegate of the object, also passing this object. For instance.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   [delegate touchesBeganInView: self withEvent: event];
}
0
source

Source: https://habr.com/ru/post/1789890/


All Articles