粗糙的iOS笔记四之绘制


</pre><p>1.<span style="white-space:pre">	</span>UIKit</p><p>2.<span style="white-space:pre">	</span>Core Animation</p><p>3.<span style="white-space:pre">	Core Graphics</span></p><p>Core Graphics  负责底层绘制 </p><p></p><p>自定义绘制带有属性的view</p><p><span style="white-space: pre;">	</span>1)属性用于将来绘制时使用 利用属性值的变化绘制的图形相应变化</p><p>重绘</p><span style="white-space:pre">	</span>1)所有的view如果不加修改,只会在视图加载后根据初始值的数据绘制一次<p><span style="white-space:pre">	</span>2)如果希望在view的size更改后重绘view,则需要修改View检查器中的contentMode为redraw</p><p><span style="white-space:pre">	</span>3)如果希望在view的属性被修改后重绘view,则需要覆盖属性的setter消息,并在消息内部调用</p><p><span style="white-space:pre">	</span>[self setNeedDisplay];  self 指需要重新绘制的view</p><p></p><p></p><p></p><pre code_snippet_id="201593" snippet_file_name="blog_20140224_1_2498563" name="code" class="objc">// Foundation  OC 写的   不需要管理内存
    // Core Foundation   C 写的   需要管理内存
 // 绘制直线   
    // Drawing 画布
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 下笔的位置
    CGContextMoveToPoint(context, 10, 10);
    // 准备要绘制的区域
    CGContextAddLineToPoint(context, rect.size.width - 10, 10);
    CGContextAddLineToPoint(context, rect.size.width - 10, rect.size.height - 10);
    CGContextAddLineToPoint(context, rect.size.width , rect.size.height);
    
    CGContextAddLineToPoint(context, 10, rect.size.height);
    CGContextAddLineToPoint(context, 10, 10);
    
    // 准备好调色板并设置填充颜色
    [[UIColor grayColor] setFill];  // OC对底层数据操作的封装
//    CGContextSetRGBFillColor(context, 0.3, 0.3, 0.3, 1.0); // 底层的操作
    
    // 针对描述好的形状区域涂色
    CGContextFillPath(context);


// 另一种绘制  直线
- (void)drawRect:(CGRect)rect
{
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:CGPointMake(10, 10)];
    [bezierPath addLineToPoint:CGPointMake(rect.size.width - 10, 10)];
    [bezierPath addLineToPoint:CGPointMake(rect.size.width - 10, rect.size.height - 20)];
    [bezierPath addLineToPoint:CGPointMake(rect.size.width, rect.size.height - 10)];
    [bezierPath addLineToPoint:CGPointMake(10, rect.size.height - 10)];
    [bezierPath closePath];
    
    [[UIColor grayColor] setFill];
    [bezierPath fill];
    
    CGContextRestoreGState(context);
}

// 绘制曲线
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    
    CGPoint point = CGPointMake(rect.size.width / 2, rect.size.height / 2);
    UIBezierPath * path = [UIBezierPath bezierPath];
    //
    [path addArcWithCenter:point        // 绘制的起点  或 中心点
                    radius:14           // 半径
                startAngle:M_PI_2 * 3   // 起始弧度
                  endAngle:M_PI_2 * 3 + self.value * M_PI * 2   // 结束 弧度
                 clockwise:YES];        // YES 为顺时针  NO 为逆时针
    // 设置边框颜色
    [[UIColor blueColor] setStroke];
    // 涂色
    [path stroke];
//    [path closePath];
    
    CGContextRestoreGState(context);
}

// 绘制气泡
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:CGPointMake(10, 10)];
//    [bezierPath addLineToPoint:CGPointMake(rect.size.width - 10, 10)];
    [bezierPath addArcWithCenter:CGPointMake(rect.size.width - 20, 20)
                                       radius:10
                                 startAngle:M_PI_2 * 3
                                  endAngle:M_PI * 2
                                  clockwise:YES];
//    [bezierPath addLineToPoint:CGPointMake(rect.size.width - 10, 20)];
//    [bezierPath addLineToPoint:CGPointMake(rect.size.width - 10, rect.size.height - 20)];
    [bezierPath addArcWithCenter:CGPointMake(rect.size.width - 20, rect.size.height - 20)
                          radius:10
                      startAngle:0
                        endAngle:M_PI_2
                       clockwise:YES];
//    [bezierPath addLineToPoint:CGPointMake(rect.size.width - 20, rect.size.height - 10)];
//    [bezierPath addLineToPoint:CGPointMake(30, rect.size.height - 10)];
    [bezierPath addArcWithCenter:CGPointMake(30, rect.size.height - 20)
                          radius:10
                      startAngle:M_PI_2
                        endAngle:M_PI
                       clockwise:YES];
//    [bezierPath addLineToPoint:CGPointMake(20, rect.size.height - 20)];
    [bezierPath addLineToPoint:CGPointMake(20, 20)];
    [bezierPath closePath];
    // 设置填充色
    [[UIColor grayColor] setFill];
    // 涂色
    [bezierPath fill];
    CGContextRestoreGState(context);
}

// 绘制字符串
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    
    NSDictionary *attributes = @{
       NSFontAttributeName : [UIFont systemFontOfSize:30],
       NSForegroundColorAttributeName : [UIColor redColor]};
    
    NSString *str1 = @"Hello World....";
    [str1 drawAtPoint:CGPointMake(10, 20) withAttributes:attributes];
    NSString *str2 = @"Long long long long String, And Very Long long long long.";
    [str2 drawInRect:CGRectMake(10, 50, 300, 200) withAttributes:attributes];
    CGContextRestoreGState(context);
    
    NSString *str3 = @"内容未知的字符串";
 
    CGRect r = [str3 boundingRectWithSize:CGSizeMake(220, 600) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    r.origin.x = 10;
    r.origin.y = 200;
    NSLog(@"%lf, %lf", r.size.width, r.size.height);
    [str3 drawInRect:r withAttributes:attributes];
}
B / S    Browser / Server

C / S    Client / Server

<span style="color: rgb(51, 102, 255);">// Http请求种类
//	get 参数放在url 上
//	post 参数放在http请求体内
// url 内的参数限制
// 1. 长度有限
// 2. url上的编码有限制 ASCII</span>

NSURL * url = [NSURL URLWithString:@""];
    NSURLRequest * request = [[NSURLRequest alloc] initWithURL:url];
    NSData * data =
    [NSURLConnection sendSynchronousRequest:request
                          returningResponse:nil
                                      error:nil];
    NSString * info =
    [[NSString alloc] initWithData:data
                          encoding:NSUTF8StringEncoding];
    NSLog(@"%@",info);

给大家推荐一个swift视频教程,后续会不断更新,下载文件需要验证多次,有点耐心哦!

从OC转移到swift:环境与变量

swift进阶可能值

swift枚举扩展泛型

类初始化属性方法

元组闭包

变量字符串集合循环


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值