UI手势二

//首先是ViewController.h的实现文件

import “ViewController.h”

import “DrawView.h”

import “Quart.h”

import “appView.h”

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    /*
    画图
    */
    //第一种
    //创建一个画布
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), NO, 1.0);
    //拿到创建画布使之成为当前画布
    CGContextRef conref = UIGraphicsGetCurrentContext();
    // //画一个圆
    // CGContextAddEllipseInRect(conref, CGRectMake(0, 0, 100, 100));
    //画一个矩形
    CGContextAddRect(conref, CGRectMake(0, 0, 100, 100));
    //画线
    CGContextMoveToPoint(conref, 10, 10);
    CGContextAddLineToPoint(conref, 100, 100);
    //设置填充颜色
    CGContextSetFillColorWithColor(conref, [UIColor redColor].CGColor);
    //设置画笔
    CGContextSetStrokeColorWithColor(conref, [UIColor redColor].CGColor);
    //设置画笔粗细
    CGContextSetLineWidth(conref, 1.0);
    //开始画
    CGContextFillPath(conref);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //结束绘画
    UIGraphicsEndPDFContext();
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    imageView.frame = CGRectMake(60, 60, 100, 100);
    // [self.view addSubview:imageView];
    //第二种
    DrawView *drawView = [[DrawView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:drawView];
    Quart *quart = [[Quart alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:quart];
    appView *appview = [[appView alloc] initWithFrame:self.view.bounds];
    // appview.backgroundColor = [UIColor greenColor];
    [self.view addSubview:appview];
    }

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end
//创建三个继承UIView的类

import “DrawView.h”

@implementation DrawView
- (void)drawRect:(CGRect)rect {
//UIBezierPath
UIBezierPath * p = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 20, 80, 80) cornerRadius:40];
[[UIColor blueColor] setFill];
[p fill];
UIBezierPath *p1 = [UIBezierPath bezierPathWithRect:CGRectMake(100, 20, 100, 120)];
[[UIColor yellowColor] setFill];
[p1 fill];
UIBezierPath *p2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(300, 300) radius:30 startAngle:M_PI/3 endAngle:M_PI clockwise:YES];
[[UIColor cyanColor] setFill];
[p2 fill];
// UIBezierPath *p3 = [UIBezierPath bezierPathWithCGPath:曲线
// UIBezierPath *p4 = [UIBezierPath bezierPathWithOvalInRect:<#(CGRect)#>]椭圆
//在ios7新加的API
NSString *text = @”666”;
UIFont *font = [UIFont systemFontOfSize:14];
[text drawAtPoint:CGPointMake(100, 100) withAttributes:@{NSFontAttributeName:font}];
UIImage *image = [UIImage imageNamed:@”281BD0EE-AD73-4DBA-824A-8D3817D890FF”];
[image drawInRect:CGRectMake(100, 300, 100, 100)];

}

@end

import “Quart.h”

@implementation Quart

  • (void)drawRect:(CGRect)rect {
    // Drawing code

    CGContextRef context = UIGraphicsGetCurrentContext();//获取画板
    // CGContextSetRGBStrokeColor(<#CGContextRef context#>, <#CGFloat red#>, <#CGFloat green#>, <#CGFloat blue#>, <#CGFloat alpha#>)
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);//设置画笔颜色
    CGContextSetLineWidth(context, 1.5);//设置画笔粗细
    // CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);填充色

    //画无填充圆
    CGContextAddArc(context, 100, 20, 15, 0, 2*M_PI, YES);
    CGContextDrawPath(context, kCGPathStroke);
    //画填充圆
    CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);
    CGContextAddArc(context, 100, 20, 15, 0, 2*M_PI, YES);
    CGContextDrawPath(context, kCGPathStroke);
    //画弧形
    CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);
    CGContextAddArc(context, 100, 20, 15, 0, M_PI, YES);
    CGContextDrawPath(context, kCGPathStroke);
    //画线
    CGPoint points[2];
    points[0] = CGPointMake(70, 70);
    points[1] = CGPointMake(170, 170);
    CGContextAddLines(context, points, 2);
    CGContextDrawPath(context, kCGPathStroke);
    CGContextSetRGBStrokeColor(context, 0.3, 0.5, 0.5, 1.0);//设置画笔颜色
    CGContextSetLineWidth(context, 1);//设置画笔粗
    //画线2
    CGContextMoveToPoint(context, 180, 170);//将画笔移动到某一点
    CGContextStrokePath(context);//绘制路径
    //两点之间画弧线
    CGContextMoveToPoint(context, 80, 70);
    CGContextAddArcToPoint(context, 148, 80, 160, 90, 10);

}

@end
//appView类的头文件

import

import “appView.h”

@implementation appView

  • (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
    self.userInteractionEnabled = YES;
    self.multipleTouchEnabled = YES;//允许多指交互
    }
    return self;
    }
  • (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);//设置画笔颜色
    CGContextSetLineWidth(context, 5);//设置线条的粗细
    CGContextMoveToPoint(context, [points[0] CGPointValue].x, [points[0] CGPointValue].y );
    for (int i = 1;i < points.count; i++) {
    CGContextAddLineToPoint(context, [points[i]CGPointValue].x, [points[i] CGPointValue].y);
    }
    CGContextStrokePath(context);
    }
  • (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event{
    points = [NSMutableArray array];
    }
  • (void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    [points addObject:[NSValue valueWithCGPoint:point]];
    [self setNeedsDisplay];//调用drawRect方法

}
- (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event{
}
- (void)touchesCancelled:(NSSet )touches withEvent:(UIEvent )event{

}
@end
//当然了,还要添加png格式的图片一张
//最后的效果是,画图的时候断点不会消失

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值