Reason for - (void)cancelTrackingWithEvent:(UIEvent *)event getting called If I am using MSSlideNavigationController?

StackOverflow https://stackoverflow.com/questions/21299604

  •  01-10-2022
  •  | 
  •  

سؤال

I tried to use custom UIControl in my view controller. My custom class which subclasses the UIControl and allocate the instance for my custom control and adding in to my view controller's view by following code

 CustomControl *customControl = [[CustomControl alloc]initWithFrame:CGRectMake(44, 388, 235, 160)];
 [self.view addSubview:customControl];

Then I am implementing the following delegate methods in CustomControl

- (BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;
- (BOOL) continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;
- (void) endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;
- (void)cancelTrackingWithEvent:(UIEvent *)event

The problem I am facing is when I am tracking inside the control -(void)cancelTrackingWithEvent:(UIEvent *)event get called after that - (BOOL) continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event method doesn't get called. So why this method - (void)cancelTrackingWithEvent:(UIEvent *)event getting called...

I read the document that says, // event may be nil if cancelled for non-event reasons, e.g. removed from window but can't understand the exact scernario Thanks in advance.

Always I return YES only. The delegates I have implemented are:

- (BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    [super beginTrackingWithTouch:touch withEvent:event];
    NSLog(@"Touch begins");
    return YES;
}

- (BOOL) continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    [super continueTrackingWithTouch:touch withEvent:event];
    NSLog(@"Touch continous");
    return YES;
}

- (void) endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    [super endTrackingWithTouch:touch withEvent:event];
    NSLog(@"Touch ends");
}

- (void)cancelTrackingWithEvent:(UIEvent *)event
{
    NSLog(@"Touch cancelled due to remove from window");
}
هل كانت مفيدة؟

المحلول 2

I found out the problem. I used MSSlideNavigationController which overrides - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

So when I track, the touch passed to MSSlideNavigationController. To solve this I changed the (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch' inMSSlideNavigationController` class that returns NO when the tracking comes from my CustomUIControl. I set tag 9999 to my customcontrol

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    if (touch.view.tag == 9999) {
        return NO;
    }
    return YES;
}

نصائح أخرى

maybe this can help. I once looked up UISlider's header file, and found out it used this method.

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;

Description
Asks the view if the gesture recognizer should be allowed to continue tracking touch events. Subclasses may override this method and use it to prevent the recognition of particular gestures. For example, the UISlider class uses this method to prevent swipes parallel to the slider’s travel direction and that start in the thumb. At the time this method is called, the gesture recognizer is in the UIGestureRecognizerStatePossible state and thinks it has the events needed to move to the UIGestureRecognizerStateBegan state. The default implementation of this method returns YES.

(https://developer.apple.com/documentation/uikit/uiview/1622460-gesturerecognizershouldbegin?language=objc)

In order for continueTrackingWithTouch:withEvent to be called, I think you need to return YES from both beginTrackingWithTouch:withEvent and continueTrackingWithTouch:withEvent. If you do not return YES, then you are saying that you do not want the touch to be tracked any longer, and continueTrackingWithTouch:withEvent will no longer be called.

One reason that cancelTracking... would be called, is if you return to the Home Screen while a touch is being tracked.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top