在自定义视图中处理 UITapGestureRecognizer
我有一个自定义视图类。在我的视图控制器中,我在此视图对象上添加了一个点击手势识别器。现在,在点击手势的处理程序中,我正在视图对象上设置一个属性,我试图在视图类的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有任何东西调用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.