簡體   English   中英

iOS:創建一個像iPhone日歷列表一樣的背景顏色的圓圈

[英]iOS: Make a circle with background color like the iPhone calendar list

我正試圖在我的桌面單元格左側添加一個圓圈,但無法弄清楚最好的方法。 我嘗試添加

CGContextRef context= UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetAlpha(context, 0.5);
CGContextFillEllipseInRect(context, CGRectMake(10.0, 10.0, 10.0, 10.0));

CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextStrokeEllipseInRect(context, CGRectMake(10.0, 10.0, 10.0, 10.0));

到我的cellForRowAtIndexPath但保持無效的上下文錯誤。 這是我的cellForRowAtIndexPath。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"AppointmentCell";
    NSDictionary *appointment = [[self.appointments objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    int minutes = (int)[[NSString stringWithFormat:@"%@", [appointment objectForKey:@"start_time"]] integerValue];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"h:mma"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    NSDate *midnight = [NSDate dateWithTimeIntervalSince1970:0];
    NSDate *newDate = [midnight dateByAddingTimeInterval:minutes*60];

    NSString *newTime = [dateFormatter stringFromDate:newDate];
    dateFormatter = nil;

    cell.textLabel.text = newTime;
    cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
    cell.detailTextLabel.text = [appointment objectForKey:@"reason"];

    return cell;
}

如何像iPhone的日歷列表視圖一樣添加顏色的圓圈?

更新

您渲染圓圈的代碼很好,您只需將其置於UIView子類中以使其正常工作。

@interface CircleView : UIView{
}
@implementation CircleView{

- (void)drawRect:(CGRect)rect{
    CGContextRef context= UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetAlpha(context, 0.5);
    CGContextFillEllipseInRect(context, CGRectMake(0,0,self.frame.size.width,self.frame.size.height));

    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextStrokeEllipseInRect(context, CGRectMake(0,0,self.frame.size.width,self.frame.size.height));
}

}

然后,您可以將圓形視圖添加到表格單元格中,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //...
    //Your existing code
    CGRect positionFrame = CGRectMake(10,10,10,10);
    CircleView * circleView = [[CircleView alloc] initWithFrame:positionFrame];
    [cell.contentView addSubview:circleView];
    [circleView release];

    return cell;
}

使用位置框架,直到它符合您的需要。

tableView:cellForRowAtIndexPath:是創建單元格的位置,但這不是繪圖發生的位置。 如果你想在tableViewCell中進行自定義繪圖,你需要UITableViewCell ,覆蓋drawRect: ,並將你的繪圖代碼放在那里。

或者,您可以將tableViewCell的imageView的UIImage設置為圓形圖片。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM