iOS入门(二十八)事件处理
2015-08-11 16:46
344 查看
事件处理 绘图
UIResponder : UIView UIViewController UIApplication
事件响应顺序
1、设备
2、当前app
3、delegate
4、UIWindow
5、VIewController
6、view
7、subview
UIImageView UILabel 默认不响应任何事件。
NSValue : 将结构体转换成对象
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor cyanColor].CGColor);
CGContextSetLineWidth(context, 5);
for (int i = 0 ; i < [_lineArray count] ; i ++ ) {
//取出一条线(点得数组)
NSMutableArray * pointArr = [_lineArray objectAtIndex:i];
if ([pointArr count] > 0 ) {
for (int j = 0 ; j < [pointArr count] -1; j ++ ) {
NSValue * a = [pointArr objectAtIndex:j ];
CGPoint pointA = [a CGPointValue];
NSValue * b = [pointArr objectAtIndex:j + 1];
CGPoint pointB = [b CGPointValue];
CGContextMoveToPoint(context, pointA.x, pointA.y);
CGContextAddLineToPoint(context, pointB.x, pointB.y);
}
}
}
CGContextStrokePath(context);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//每次点击屏幕的时候,创建一条线的数组
NSMutableArray * points = [NSMutableArray array];
//把创建好的线添加到线的数组里面
[self.lineArray addObject:points];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//获得最新的线
NSMutableArray * pointArr = [self.lineArray lastObject];
//获得当前经过的点
//获得UITouch
UITouch * touch = [touches anyObject];
//获得touch中存储的点
CGPoint point = [touch locationInView:self];
//把CGPoint 转换成对象(NSValue)
NSValue * value= [NSValue valueWithCGPoint:point];
//将接触获得的点添加到当前正在绘制的线中
[pointArr addObject:value];
//强制UIView调用drawRect方法
[self setNeedsDisplay];
}
//context包含UIView需要的各种信息
// CGContextRef context = UIGraphicsGetCurrentContext();
// //1、设置线的颜色
// CGContextSetStrokeColorWithColor(context, [UIColor cyanColor].CGColor);
// //2、设置线的粗细
// CGContextSetLineWidth(context, 6);
// //3、设置线段的起点
// CGContextMoveToPoint(context, 20, 20);
// //4、设置线段的终点
//// CGContextAddLineToPoint(context, 220, 220);
//
// //绘制椭圆
//// CGContextAddEllipseInRect(context, CGRectMake(20, 20, 200, 200));
// //绘制方形
//// CGContextAddRect(context, CGRectMake(20, 20, 200, 200));
// //绘制曲线 参数: context 曲线参考点坐标,结束点坐标 ( 起点为之前停止的地方)
// CGContextAddQuadCurveToPoint(context, 0, 200, 320, 220);
//
// //5、绘制设置好的线
// CGContextStrokePath(context);
// CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
// CGContextSetLineWidth(context, 5.2f);
//
// UIColor * aColor2 = [UIColor cyanColor];//天蓝色
// CGContextSetFillColorWithColor(context, aColor2.CGColor);//填充颜色
// CGContextAddArc(context, 150, 50, 30, 50, 60, 0);//填充圆
// CGContextFillPath(context);
//
// CGContextAddArc(context, 40, 280, 30, 50, 60, 0);//空心圆
// CGContextStrokePath(context);
//
//
// UIColor * aColor = [UIColor cyanColor];//天蓝色
// CGContextSetFillColorWithColor(context, aColor.CGColor);//填充颜色
// CGContextFillRect(context,CGRectMake(10, 180, 150, 50));//填充框
//
// CGContextStrokeRect(context,CGRectMake(10, 120, 150, 50));//空心框
//
//
// UIColor * aColor1 = [UIColor grayColor];//灰色
// CGContextSetFillColorWithColor(context, aColor1.CGColor);//填充颜色
// CGContextAddEllipseInRect(context, CGRectMake(90, 280, 60, 30)); //椭圆
// CGContextDrawPath(context, kCGPathFillStroke);
//
//
// //画三角形
// CGPoint sPoints[3];//坐标点
// sPoints[0] =CGPointMake(165, 285);//坐标1
// sPoints[1] =CGPointMake(195, 285);//坐标2
// sPoints[2] =CGPointMake(195, 225);//坐标3
// CGContextAddLines(context, sPoints, 3);//添加线
// CGContextClosePath(context);//封起来
// CGContextDrawPath(context, kCGPathFillStroke); //根据坐标绘制路径
// CGContextStrokePath(context);//开始绘制
//上午练习
//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor yellowColor]];
// NSLog(@"%@",touches);
// UITouch * touch = [touches anyObject];
// NSLog(@"%d",touch.tapCount);
//}
//-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor cyanColor]];
//}
//-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor blueColor]];
//}
//-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor greenColor]];
// NSLog(@"lallalalalala");
//}
//-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor purpleColor]];
//}
[self setBackgroundColor:[UIColor yellowColor]];
NSLog(@"%@",touches);
UITouch * touch = [touches anyObject];
NSLog(@"%d",touch.tapCount);
UIResponder : UIView UIViewController UIApplication
事件响应顺序
1、设备
2、当前app
3、delegate
4、UIWindow
5、VIewController
6、view
7、subview
UIImageView UILabel 默认不响应任何事件。
NSValue : 将结构体转换成对象
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor cyanColor].CGColor);
CGContextSetLineWidth(context, 5);
for (int i = 0 ; i < [_lineArray count] ; i ++ ) {
//取出一条线(点得数组)
NSMutableArray * pointArr = [_lineArray objectAtIndex:i];
if ([pointArr count] > 0 ) {
for (int j = 0 ; j < [pointArr count] -1; j ++ ) {
NSValue * a = [pointArr objectAtIndex:j ];
CGPoint pointA = [a CGPointValue];
NSValue * b = [pointArr objectAtIndex:j + 1];
CGPoint pointB = [b CGPointValue];
CGContextMoveToPoint(context, pointA.x, pointA.y);
CGContextAddLineToPoint(context, pointB.x, pointB.y);
}
}
}
CGContextStrokePath(context);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//每次点击屏幕的时候,创建一条线的数组
NSMutableArray * points = [NSMutableArray array];
//把创建好的线添加到线的数组里面
[self.lineArray addObject:points];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//获得最新的线
NSMutableArray * pointArr = [self.lineArray lastObject];
//获得当前经过的点
//获得UITouch
UITouch * touch = [touches anyObject];
//获得touch中存储的点
CGPoint point = [touch locationInView:self];
//把CGPoint 转换成对象(NSValue)
NSValue * value= [NSValue valueWithCGPoint:point];
//将接触获得的点添加到当前正在绘制的线中
[pointArr addObject:value];
//强制UIView调用drawRect方法
[self setNeedsDisplay];
}
//context包含UIView需要的各种信息
// CGContextRef context = UIGraphicsGetCurrentContext();
// //1、设置线的颜色
// CGContextSetStrokeColorWithColor(context, [UIColor cyanColor].CGColor);
// //2、设置线的粗细
// CGContextSetLineWidth(context, 6);
// //3、设置线段的起点
// CGContextMoveToPoint(context, 20, 20);
// //4、设置线段的终点
//// CGContextAddLineToPoint(context, 220, 220);
//
// //绘制椭圆
//// CGContextAddEllipseInRect(context, CGRectMake(20, 20, 200, 200));
// //绘制方形
//// CGContextAddRect(context, CGRectMake(20, 20, 200, 200));
// //绘制曲线 参数: context 曲线参考点坐标,结束点坐标 ( 起点为之前停止的地方)
// CGContextAddQuadCurveToPoint(context, 0, 200, 320, 220);
//
// //5、绘制设置好的线
// CGContextStrokePath(context);
// CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
// CGContextSetLineWidth(context, 5.2f);
//
// UIColor * aColor2 = [UIColor cyanColor];//天蓝色
// CGContextSetFillColorWithColor(context, aColor2.CGColor);//填充颜色
// CGContextAddArc(context, 150, 50, 30, 50, 60, 0);//填充圆
// CGContextFillPath(context);
//
// CGContextAddArc(context, 40, 280, 30, 50, 60, 0);//空心圆
// CGContextStrokePath(context);
//
//
// UIColor * aColor = [UIColor cyanColor];//天蓝色
// CGContextSetFillColorWithColor(context, aColor.CGColor);//填充颜色
// CGContextFillRect(context,CGRectMake(10, 180, 150, 50));//填充框
//
// CGContextStrokeRect(context,CGRectMake(10, 120, 150, 50));//空心框
//
//
// UIColor * aColor1 = [UIColor grayColor];//灰色
// CGContextSetFillColorWithColor(context, aColor1.CGColor);//填充颜色
// CGContextAddEllipseInRect(context, CGRectMake(90, 280, 60, 30)); //椭圆
// CGContextDrawPath(context, kCGPathFillStroke);
//
//
// //画三角形
// CGPoint sPoints[3];//坐标点
// sPoints[0] =CGPointMake(165, 285);//坐标1
// sPoints[1] =CGPointMake(195, 285);//坐标2
// sPoints[2] =CGPointMake(195, 225);//坐标3
// CGContextAddLines(context, sPoints, 3);//添加线
// CGContextClosePath(context);//封起来
// CGContextDrawPath(context, kCGPathFillStroke); //根据坐标绘制路径
// CGContextStrokePath(context);//开始绘制
//上午练习
//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor yellowColor]];
// NSLog(@"%@",touches);
// UITouch * touch = [touches anyObject];
// NSLog(@"%d",touch.tapCount);
//}
//-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor cyanColor]];
//}
//-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor blueColor]];
//}
//-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor greenColor]];
// NSLog(@"lallalalalala");
//}
//-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor purpleColor]];
//}
[self setBackgroundColor:[UIColor yellowColor]];
NSLog(@"%@",touches);
UITouch * touch = [touches anyObject];
NSLog(@"%d",touch.tapCount);
相关文章推荐
- ios 动画的时候 如果加阴影 会卡顿的
- ios 8行代码教你搞定导航控制器全屏滑动返回效果
- iOS入门(二十六)iOS程序启动流程
- iOS入门(二十五)基础控件
- 教你轻松看懂 iOS9 新功能
- iOS入门(二十三)内存管理
- iOS入门(二十二)属性
- 更新日志 - 关于 iOS9 设备的安装及其他优化
- iOS开发-- 使用VVDocumenter-Xcode添加代码注释
- iOS入门(二十一)类的扩展
- iOS入门(二十)字典
- iOS研发之静态库的封装
- iOS入门(十九)NSString NSArray NSDictionary
- iOS入门(十八)OC中常用的几种传值方式
- iOS开发-- 使用NSNumber将int、float、long等数据类型加入到数组或字典中
- ios性能测试
- IOS--商品的添加与删除
- IOS--OHAttributedLabel
- 【iOS学习笔记】strong weak retain assign
- iOS 支付宝的集成与遇到的问题