【问题标题】:UIScreenEdgePanGestureRecognizer in Objective-CObjective-C 中的 UIScreenEdgePanGestureRecognizer
【发布时间】:2020-07-24 02:39:25
【问题描述】:

我正在学习objective-c,不知道这段代码是做什么的。我了解 UIScreenEdgePanGestureRecognizer 的作用。但是,我对这段代码对 UIScreenEdgePanGesture 的作用感到困惑。

UIScreenEdgePanGestureRecognizer *rightRecog = 
[[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(resetChart:)];
[rightRecog setEdges:UIRectEdgeRight];
[rightRecog setCancelsTouchesInView:YES];

[self.chartView addGestureRecognizer:rightRecog];

UIScreenEdgePanGestureRecognizer *leftRecog = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(resetChart:)];

[leftRecog setEdges:UIRectEdgeLeft];
[leftRecog setCancelsTouchesInView:YES];

[self.chartView addGestureRecognizer:leftRecog];

【问题讨论】:

    标签: ios objective-c uipangesturerecognizer


    【解决方案1】:

    这是一个用于定义UIPanGestureRecognizer 的代码模式,用于从屏幕中心的右侧和左侧检查设备屏幕边缘上的手指/笔触摸,但会取消任何触摸事件同时发生在视图内部。

    识别器绑定到self.chartView 以观察这些手势。

    它还定义了一个方法@selector(resetChart:),当手指滑入图表视图的一侧时,该方法将采取行动(动作)。但是 -resetChart: 方法将是您定义识别器的 ViewController 类或视图类的一部分,其中定义为 (self)。

    UIScreenEdgePanGestureRecognizer *rightRecog = 
    [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(resetChart:)];
    [rightRecog setEdges:UIRectEdgeRight];
    [rightRecog setCancelsTouchesInView:YES];
    [self.chartView addGestureRecognizer:rightRecog];
    

    上面的代码是从右侧设置识别器。 下面的代码对左侧执行相同的操作,但会触发相同的方法。

    UIScreenEdgePanGestureRecognizer *leftRecog = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(resetChart:)];
    [leftRecog setEdges:UIRectEdgeLeft];
    [leftRecog setCancelsTouchesInView:YES];
    [self.chartView addGestureRecognizer:leftRecog];
    

    所以它可以像这样融化在一起..

    UIScreenEdgePanGestureRecognizer *rightLeftRecog =
    [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(resetChart:)];
    [rightLeftRecog setEdges:UIRectEdgeRight|UIRectEdgeLeft];
    [rightLeftRecog setCancelsTouchesInView:YES];
    [self.chartView addGestureRecognizer:rightLeftRecog];
    

    因为-resetChart: 也在做同样的事情。

    看起来像

    -(void)resetChart:(UIScreenEdgePanGestureRecognizer*)gesture {
        //do some stuff with the gesture or amount of fingers used.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-14
      • 1970-01-01
      • 2012-11-28
      • 2012-07-17
      相关资源
      最近更新 更多