繁体   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