我有一个自定义视图类。在我的视图控制器中,我在这个视图对象上添加了一个 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
}
}