Ios UITapGestureRecognitor仅在初始化和重新加载viewcontroller后工作

Ios UITapGestureRecognitor仅在初始化和重新加载viewcontroller后工作,ios,objective-c,uiviewcontroller,uitapgesturerecognizer,Ios,Objective C,Uiviewcontroller,Uitapgesturerecognizer,在我的应用程序中,我有一个带有日历的viewcontroller。同一时间只显示一个月,通过滚动可以切换月份。在日历中的某些日子,某些事件会以圆圈突出显示 月份与代码切换 - (void) nextMonth:(UISwipeGestureRecognizer *)swipe { [self.calView moveCalendarToNextMonth]; [self drawItemsForCurrentMonth]; } 方法drawItemsForCurrentMonth为每个需要突出

在我的应用程序中,我有一个带有日历的viewcontroller。同一时间只显示一个月,通过滚动可以切换月份。在日历中的某些日子,某些事件会以圆圈突出显示

月份与代码切换

- (void) nextMonth:(UISwipeGestureRecognizer *)swipe {
[self.calView moveCalendarToNextMonth];
[self drawItemsForCurrentMonth];
}
方法drawItemsForCurrentMonth为每个需要突出显示的项调用下一段代码

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

NSArray* res = [self calculateDayCirclePosition:date];

CGFloat x = [[res objectAtIndex:0] floatValue] + (DEFAULT_CELL_WIDTH-DEFAULT_CIRCLE_WIDTH-CELL_BORDER_WIDTH)/2;
CGFloat y = [[res objectAtIndex:1] floatValue] + (DEFAULT_CELL_HEIGHT-DEFAULT_CIRCLE_WIDTH)/2;

AgendaCircleView* circleView = [[AgendaCircleView alloc] initWithFrame:CGRectMake(x,y,self.circleWidth,self.circleWidth)];

circleView.date = date;
AgendaViewController* avc;

if([appDelegate.viewController.frontViewController isKindOfClass:[UINavigationController class]]) {
    UINavigationController* navVC = (UINavigationController*)appDelegate.viewController.frontViewController;
    avc = (AgendaViewController*)[navVC visibleViewController];
}

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:avc action:@selector(didSelectDate:)];

tapGesture.enabled = YES;
tapGesture.delegate = avc;
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;

[circleView addGestureRecognizer:tapGesture];
circleView.userInteractionEnabled = YES;

circleView.alpha = 0.5;
circleView.layer.cornerRadius = (self.circleWidth/2);

if(fill) {
    circleView.backgroundColor = [appDelegate.dataController.rijschoolItem getColor1];
}
else {
    circleView.layer.borderWidth = 1.0f;
    circleView.backgroundColor = [UIColor clearColor];
    circleView.layer.borderColor = [UIColor whiteColor].CGColor;
}

[circleView setNeedsDisplay];

[self.calendarContainer addSubview:circleView];

[self.calendarContainer bringSubviewToFront:circleView];

[self.calendarContainer setUserInteractionEnabled:YES];
初始化viewcontroller时,此代码工作正常。单击圆圈将触发didSelectDate方法。但是,当我更改月份时,TapGestureRecognitor不再工作。当月份发生变化时,我使用的代码与初始化viewcontroller时使用的代码完全相同。奇怪的是,当我打开另一个详细的viewcontroller并将其扔到视图堆栈上并返回时,它又开始工作了

我试着用

手势识别器:UIGestureRecognitor*手势识别器 应使用GestureRecognitor同时识别:UIGestureRecognitor*其他GestureRecognitor,但这并不能解决问题

此外,我尝试使用setNeedsLayout,但这也不起作用


有什么想法吗?

好的,我发现了问题。所以对于其他可能遇到同样问题的人来说。添加手势识别器时,必须确保视图未设置动画

我有这段代码

[UIView animateWithDuration:0.4 animations:^{
   calendar.alpha = 1.0;
}];

[self drawItemsForCurrentMonth];
变成

[UIView animateWithDuration:0.4 animations:^{
    calendar.alpha = 1.0;
} completion:^(BOOL finished) {
    if(finished) {
        [self drawItemsForCurrentMonth];
    }
}];

在动画块之后使用DrawItemsForcurntMonth方法并不一定保证该块已实际完成。幸运的是,有一个完整的区块涵盖了这一方面

您是否尝试过实现此方法:-BOOL gestureRecognizer:UIGestureRecognizer*gestureRecognizer应接收触摸:UITouch*触摸?我尝试过,但触摸也不调用此方法。我也试着将圆圈视图放在前面,但没有成功。告诉我们你从哪里调用代码。问题似乎是您在第二个月没有将手势识别器添加到视图中。我添加了其他代码