【问题标题】:add gesturerecognizer on top right side of navigation bar在导航栏的右上角添加手势识别器
【发布时间】:2022-01-05 02:35:39
【问题描述】:

我使用以下代码在 uinavigationbar 的右上角添加手势识别器,但如果我点击导航栏上的任何位置,我会得到结果。我该怎么做右上角的手势?

- (void)handleGestureForTopRightBarButtonItem:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint p = [gestureRecognizer locationInView:self.navigationController.navigationBar];
    if (CGRectContainsPoint(CGRectMake(self.navigationController.navigationController.view.width-20,0,100,self.navigationController.navigationController.view.height), p)) {
        NSLog(@"got a tap on the right side in the region i care about");
    } else {
        NSLog(@"got a tap on the right side, but not where i need it");
    }
}

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    由于 UIGestureRecognizer 正在向类对象报告,因此有几种方法可以解决此问题。 UIGestureRecognizer 并不是要在同一个视图上多次堆叠,如果这样做,除了 CPU 功率的损失和必须区分所有正在运行的识别器的大量比较代码之外,您很可能会消耗比您需要的更多的能量。但它会工作..

    a)编写代码,比较其坐标和预期值,如果它们在您想要的范围内匹配,则执行您的操作。

    b) 创建另一个对象,该对象仅存在于您想要的坐标中,并拥有自己的 UIGestureRecognizer。不理想,如上所述。

    c) 使用UIControl 的力量,它们也继承了UIViewUIResponders。

     - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;
     - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;
     - (void)endTrackingWithTouch:(nullable UITouch *)touch withEvent:(nullable UIEvent *)event; // touch is sometimes nil if cancelTracking calls through to this.
     - (void)cancelTrackingWithEvent:(nullable UIEvent *)event;
    

    d) 在没有 UIGestureRecognizer 的情况下使用 UIView 的强大功能。 顺便说一句,这基本上也适用于 CALayers,它们没有 sendAction 方法,它们不是 UIControls。

    -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
        BOOL inside = [super pointInside:point withEvent:event];
        if (inside && event.type == UIEventTypeTouches) { 
            //[super sendActionsForControlEvents:UIControlEventTouchDown];
        }
        return inside;
    }
    

    e) 编写一些继承自 UIResponder 的类,这基本上就是 UIControls 所做的,而是使用它们的 API,因此您也可以使用触摸坐标。

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        for (UITouch *touch in touches) {
            [self someCoRoutineWithTouch:touch];
        }
        //[super touchesBegan:touches withEvent:event];
    }
    -(void)touchesMoved :(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        for (UITouch *touch in touches) {
            //do some stuff per touch
        }
        //[super touchesMoved:touches withEvent:event];
    }
    -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        for (UITouch *touch in touches) {
            //do some stuff per touch
        }
        //[super touchesEnded:touches withEvent:event];
    }
    -(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        [self touchesEnded:touches withEvent:event];
    }
    

    请不要忘记UIGestureRecognizer 基本上是用于由触摸组成的手势,甚至是多次触摸,但通常不是主要用于捕捉触摸,尽管在很多示例中使用它们而不是编写适当的 UIControl。也不要忘记在设备边缘中,手势的识别受到手指大小的限制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      相关资源
      最近更新 更多