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

时间:2013-12-12 12:08:46

标签: ios iphone objective-c 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];


        }
    }

现在,我希望当用户触摸某个约会时,会显示一个detailView,其中包含有关约会的更多信息。

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

任何帮助?

亲切的问候

1 个答案:

答案 0 :(得分:0)

有几件事:

如果您将约会解析为模型类Appointment,则会更容易。 在绘制每个约会之前,计算每个约会视图的rect,然后你可以将这些数据存储在一个词典数组中以供日后使用,如下所示

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