【iOS开发】Quartz2D的简单使用

转载自: http://blog.csdn.net/ttf1993/article/details/45030771

画直线

    //拿到当前画布
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.拼接图形(路径)
    // 设置线段宽度
    CGContextSetLineWidth(ctx, 10);
    // 设置线段头尾部的样式
    CGContextSetLineCap(ctx, kCGLineCapRound);
    // 设置线段转折点的样式
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    // 设置颜色
    CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);
    // 设置一个起点
    CGContextMoveToPoint(ctx, 10, 10);
    // 添加一条线段到(100, 100)
    CGContextAddLineToPoint(ctx, 100, 100);
    // 渲染一次
    CGContextStrokePath(ctx);

画三角形

    // 1.获得画布
    CGContextRef ctx = UIGraphicsGetCurrentContext();  
    // 2.画三角形
    CGContextMoveToPoint(ctx, 0, 0);
    CGContextAddLineToPoint(ctx, 100, 100);
    CGContextAddLineToPoint(ctx, 150, 80);
    // 关闭路径(连接起点和最后一个点)
    CGContextClosePath(ctx);
    //设置颜色
    CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);
    // 3.绘制图形
    CGContextStrokePath(ctx);

画矩形

   // 1.获得画布
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  // 2.画矩形
  CGContextAddRect(ctx, CGRectMake(10, 10, 150, 100));
  // set : 同时设置为实心和空心颜色
  // setStroke : 设置空心颜色
  // setFill : 设置实心颜色
  [[UIColor whiteColor] set];
//	CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
  // 3.绘制图形
  CGContextFillPath(ctx);

画圆

  // 1.获得画布
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    // 2.画圆
    CGContextAddEllipseInRect(ctx, CGRectMake(50, 10, 100, 100));  
    CGContextSetLineWidth(ctx, 10);
    // 3.显示所绘制的东西
    CGContextStrokePath(ctx);

画圆弧

// 1.获得画布
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.画圆弧
    // x\y : 圆心
    // radius : 半径
    // startAngle : 开始角度
    // endAngle : 结束角度
    // clockwise : 圆弧的伸展方向(0:顺时针, 1:逆时针)
    CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);
    // 3.显示所绘制的东西
    CGContextFillPath(ctx);

画图片

UIImage *image = [UIImage imageNamed:@"me"];

    // 2.画图片
//    [image drawAtPoint:CGPointMake(50, 50)];
//    [image drawInRect:CGRectMake(0, 0, 150, 150)];
    [image drawAsPatternInRect:CGRectMake(0, 0, 200, 200)];

画字符串

   // 1.获得画布
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.画矩形
    CGRect cubeRect = CGRectMake(50, 50, 100, 100);
    CGContextAddRect(ctx, cubeRect);
    // 3.显示所绘制的东西
    CGContextFillPath(ctx);



    // 4.画文字
    NSString *str = @"哈哈哈哈Good morning hello hi hi hi hi";
    //    [str drawAtPoint:CGPointZero withAttributes:nil];

    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    // NSForegroundColorAttributeName : 文字颜色
    // NSFontAttributeName : 字体
    attrs[NSForegroundColorAttributeName] = [UIColor redColor];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:50];
    [str drawInRect:cubeRect withAttributes:attrs];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Quartz.NET是一个非常流行的开源任务调度框架,可以帮助开发者实现定时任务、重复任务和异步任务的调度。本文将介绍Quartz.NET V2.6.2版本的简单使用。 1. 安装Quartz.NET 可以通过NuGet包管理器安装Quartz.NET,或者在Visual Studio中使用Package Manager Console命令: ``` Install-Package Quartz -Version 2.6.2 ``` 2. 创建任务 在Quartz.NET中,任务需要实现IJob接口。下面是一个简单的任务示例: ``` public class HelloJob : IJob { public async Task Execute(IJobExecutionContext context) { await Console.Out.WriteLineAsync("Hello, Quartz.NET!"); } } ``` 在Execute方法中编写任务逻辑,例如输出一句话。 3. 创建调度器 创建调度器需要使用SchedulerFactory和IScheduler接口。下面是一个简单的调度器示例: ``` // 创建调度器工厂 ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); // 创建调度器 IScheduler scheduler = await schedulerFactory.GetScheduler(); // 开启调度器 await scheduler.Start(); ``` 4. 创建任务触发器 任务触发器需要指定任务的执行时间和执行频率。下面是一个简单的触发器示例: ``` // 创建任务触发器 ITrigger trigger = TriggerBuilder.Create() .WithIdentity("helloTrigger", "helloGroup") .StartNow() .WithSimpleSchedule(x => x .WithIntervalInSeconds(5) .RepeatForever()) .Build(); ``` 在上面的示例中,任务触发器每隔5秒钟执行一次。 5. 将任务和触发器绑定到调度器 将任务和触发器绑定到调度器需要使用JobDetail和Trigger接口。下面是一个简单的绑定示例: ``` // 创建任务 IJobDetail job = JobBuilder.Create<HelloJob>() .WithIdentity("helloJob", "helloGroup") .Build(); // 将任务和触发器绑定到调度器 await scheduler.ScheduleJob(job, trigger); ``` 6. 停止调度器 当不再需要调度器时,可以将其停止。下面是一个简单的停止示例: ``` // 停止调度器 await scheduler.Shutdown(); ``` 完整代码示例: ``` using System; using System.Threading.Tasks; using Quartz; using Quartz.Impl; namespace QuartzDemo { class Program { static async Task Main(string[] args) { // 创建调度器工厂 ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); // 创建调度器 IScheduler scheduler = await schedulerFactory.GetScheduler(); // 开启调度器 await scheduler.Start(); // 创建任务触发器 ITrigger trigger = TriggerBuilder.Create() .WithIdentity("helloTrigger", "helloGroup") .StartNow() .WithSimpleSchedule(x => x .WithIntervalInSeconds(5) .RepeatForever()) .Build(); // 创建任务 IJobDetail job = JobBuilder.Create<HelloJob>() .WithIdentity("helloJob", "helloGroup") .Build(); // 将任务和触发器绑定到调度器 await scheduler.ScheduleJob(job, trigger); // 等待5分钟 await Task.Delay(TimeSpan.FromMinutes(5)); // 停止调度器 await scheduler.Shutdown(); } } public class HelloJob : IJob { public async Task Execute(IJobExecutionContext context) { await Console.Out.WriteLineAsync("Hello, Quartz.NET!"); } } } ``` 运行程序后,将会输出“Hello, Quartz.NET!”并且每隔5秒钟输出一次,持续5分钟。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值