UI手势二
2015-08-29 17:43
316 查看
//首先是ViewController.h的实现文件
@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的类
- (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
(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类的头文件
(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格式的图片一张
//最后的效果是,画图的时候断点不会消失
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格式的图片一张
//最后的效果是,画图的时候断点不会消失
相关文章推荐
- UI手势
- UI中的控件简介
- UI 19 数据库
- ICPC 7096 A Rational Sequence
- UITextView根据键盘自适应边框
- iOS UILabel两端对齐的实现(可包括中英文/数字)
- UGUI之Canvas(画布)
- 08 java.lang.ProcessBuilder
- UGUI简述
- UIDevice通知
- UILabel的宽度自适应文字
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记45 Embed Segues
- UI 18 单例
- UI 18 UICollectionView
- 实现自适应的UI界面
- UIPageControl, UIScrollView属性及UIScrollViewDelegate详解
- IOS UICollectionView
- 1041. Be Unique (20)
- UINavigationController及界面传值
- squeeze()