在drawRect中的绘图项目上添加触摸事件



我正在制作一个日历应用程序。现在我在屏幕上这样画我的约会。

if (boolMark) {
        UIColor *blueColorMark = [UIColor colorWithRed:0 green:.62 blue:.984 alpha:0.5];
        appointmentRect.origin.x += 5;
        appointmentRect.size.width -= 10;
        NSArray *appointments = [[mark valueForKey:@"appointments"] mutableCopy];
        float lblHeight = appointmentRect.origin.y - tileHeightAdjustment + 20;
        for (NSDictionary *appointment in appointments){
            UIImage *overlayImage=[self imageWithColor:blueColorMark];
            NSString *strTitle = [NSString stringWithFormat:@"%@",[appointment valueForKey:@"app_label"]];
            if (forIpad) {
                NSLog(@"AppointmentRect Y = %f",lblHeight);
                appointmentRect.origin.y = lblHeight;
            }
            else
            {
                appointmentRect.origin.y -= 6;
            }
            appointmentRect.size.height = [self calculateHeight:strTitle andWidth:appointmentRect.size.width];
            lblHeight += appointmentRect.size.height + 5;
            [overlayImage drawInRect:appointmentRect];
            UIFont *f3 =[UIFont fontWithName:@"MyriadPro-Regular" size:10];
            [strTitle drawInRect: appointmentRect
                   withFont: f3
              lineBreakMode:NSLineBreakByWordWrapping
                  alignment: NSTextAlignmentCenter];

        }
    }

现在,当用户触摸某个约会时,我希望显示一个详细信息查看有关该约会的更多信息。

我现在的问题是,当用户点击某个约会(某个overlayImage)时,如何设置动作?

有什么帮助吗?

亲切问候

两件事:

如果您将约会解析为模型类Appointment,则会更容易。在绘制每个约会之前,计算每个约会视图的rect,然后可以将数据存储在一组字典中,以备以后使用,类似于

@[@{@"appointmentKey" : appointmentObject1, @"frameKey" : [NSValue valueWithCGRect:calculatedRect1]},
  @{@"appointmentKey" : appointmentObject2, @"frameKey" : [NSValue valueWithCGRect:calculatedRect2]}];
  • 您稍后将使用它来绘制和查找正确的约会
  • 制作一个方法Appointment* appointmentForPoint:(CGPoint point),迭代上层数组中的所有字典并检查CGRectContainsPoint(rect, point);如果为true,则返回与该词典关联的约会
  • 将敲击手势识别器添加到视图并调用appointmentForPoint:以获得正确的约会

最新更新