1
我需要在一些UICollectionViewCell
中繪製一個圓。用不同的彩色邊框和背景顏色圈。我的代碼是 。UICollectionViewCell drawRect:問題
UICollectionViewController
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CalendarCell *cell;
firstDayInMonth = [self dayWeekStart:[self getDateFromItem:dateFromStart section:indexPath.section row:1]];
if (indexPath.row < firstDayInMonth) {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellCalendarEmpty" forIndexPath:indexPath];
} else {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellCalendar" forIndexPath:indexPath];
if ([self compareOnlyDate:[self getDateFromItem:dateFromStart section:indexPath.section row:indexPath.item-firstDayInMonth] date2:[self getDateLocaleTimeZone:[NSDate date]]] == 1) {
cell.bgColor = [UIColor blueColor];
cell.titleLabel.textColor = [UIColor whiteColor];
cell.drawCircle = [NSNumber numberWithInt:1];
toDaySection = indexPath.section;
} else {
cell.bgColor = [UIColor whiteColor];
cell.drawCircle = [NSNumber numberWithInt:0];
cell.titleLabel.textColor = [UIColor lightGrayColor];
}
cell.titleLabel.text = [NSString stringWithFormat:@"%i", indexPath.row-firstDayInMonth+1];
}
return cell;
}
UICollectionViewCell
- (void)drawRect:(CGRect)rect
{
if ([self.drawCircle integerValue] == 1) {
[self drawCircl:0.0 end:0.5 color:[UIColor blueColor] bgColor:self.bgColor];
[self drawCircl:0.5 end:1.0 color:[UIColor redColor] bgColor:self.bgColor];
}
}
- (void) drawCircl:(float)start end:(float)end color:(UIColor*)color bgColor:(UIColor*)bgColor{
context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 0.8);
CGFloat startAngle = start * 2 * M_PI - M_PI/2;
CGFloat endAngle = end * 2 * M_PI - M_PI/2;
CGContextAddArc(context, 15, 15, 14, startAngle, endAngle, NO);
CGContextSetFillColorWithColor(context, bgColor.CGColor);
CGContextSetStrokeColorWithColor(context, color.CGColor);
CGContextDrawPath(context, kCGPathFillStroke);
}
滾動時,圓畫在不同的小區。 cell.titleLabel
始終正確顯示。爲什麼在不應該在細胞中繪製的圓圈?