1

我有一个自定义视图类。在我的视图控制器中,我在这个视图对象上添加了一个 Tap 手势识别器。现在,在点击手势的处理程序中,我正在我的视图对象上设置一个属性,我试图在我的视图类的 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
}
}
4

1 回答 1

2

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.

于 2012-02-22T21:30:25.460 回答