在自定义视图中处理 UITapGestureRecognizer

发布于 2025-01-08 08:52:37 字数 1260 浏览 0 评论 0原文

我有一个自定义视图类。在我的视图控制器中,我在此视图对象上添加了一个点击手势识别器。现在,在点击手势的处理程序中,我正在视图对象上设置一个属性,我试图在视图类的 drawRect 中获取该属性。现在,令人惊讶的是,当我在“handleGesture”中打印视图对象时“drawRect”,我得到两个不同的对象。因此,drawRect 内的 if 条件无法执行。可能是什么原因?

它不处于 UIGestureRecognizerStateBegan 状态。它总是出现在 UIGestureRecognizerStateEnded 内部。

// Adding Gesture in my view
 MyCustomView *customView= [[[MyCustomView alloc] init] autorelease];
UIGestureRecognizer *GestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
        [customView addGestureRecognizer:GestureRecognizer];
        [GestureRecognizer release];

// Handling tap on my view
- (void)handleGesture:(UIGestureRecognizer *)GestureRecognizer; {
    MyCustomView *aView= (MyCustomView *)GestureRecognizer.view;
switch (iGestureRecognizer.state) {
    case UIGestureRecognizerStateBegan:
        NSLog(@"Began");
        [aView setNeedsDisplay];
        aView.touchDown = YES;
        break;
    case UIGestureRecognizerStateEnded:    
        NSLog(@"Ended");
        aView.touchDown = NO;
        [aView setNeedsDisplay];
        break;
    default:
        break;
}
}

// Inside my view class
- (void)drawRect:(CGRect)iRect {
    if (self.touchDown) {
// Do something here
}
}

I have a custom view class. Inside my view controller I add a Tap gesture recognizer on this view object. Now, in the handler of tap gesture I am setting up a property on my view object which I am trying to fetch in the drawRect of my view class. Now, surprisingly when I print view objects in my "handleGesture" & "drawRect", I get two different objects. Because of this my if condition inside drawRect do not get execute. What could be the reason?

It do not come in state UIGestureRecognizerStateBegan. It always coming inside UIGestureRecognizerStateEnded.

// Adding Gesture in my view
 MyCustomView *customView= [[[MyCustomView alloc] init] autorelease];
UIGestureRecognizer *GestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
        [customView addGestureRecognizer:GestureRecognizer];
        [GestureRecognizer release];

// Handling tap on my view
- (void)handleGesture:(UIGestureRecognizer *)GestureRecognizer; {
    MyCustomView *aView= (MyCustomView *)GestureRecognizer.view;
switch (iGestureRecognizer.state) {
    case UIGestureRecognizerStateBegan:
        NSLog(@"Began");
        [aView setNeedsDisplay];
        aView.touchDown = YES;
        break;
    case UIGestureRecognizerStateEnded:    
        NSLog(@"Ended");
        aView.touchDown = NO;
        [aView setNeedsDisplay];
        break;
    default:
        break;
}
}

// Inside my view class
- (void)drawRect:(CGRect)iRect {
    if (self.touchDown) {
// Do something here
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2025-01-15 08:52:37

没有任何东西调用drawRect方法。您不想直接执行此操作,但在您的handleGesture方法中,您可以调用[aView setNeedsDisplay],并且您的视图的drawRect将在下一个绘图周期中被调用。

There is nothing calling the drawRect method. You don't want to do this directly, but in your handleGesture method, you could put in a call to [aView setNeedsDisplay] and your view's drawRect will get called in the next drawing cycle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文