触摸事件
来源:互联网 发布:南京市软件行业协会 编辑:程序博客网 时间:2024/04/30 16:49
iOS中的事件可以分为3大类型
触摸事件 加速计事件 远程控制事件
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event;
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event;
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event;
- (void)motionBegan:(UIEventSubtype)motionwithEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motionwithEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motionwithEvent:(UIEvent *)event;
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent *)event
提示:touches中存放的都是UITouch对象
@property(nonatomic,readonly,retain)UIWindow *window;
@property(nonatomic,readonly,retain)UIView *view;
@property(nonatomic,readonly)NSUInteger tapCount;
@property(nonatomic,readonly)NSTimeInterval timestamp;
@property(nonatomic,readonly)UITouchPhase phase;
UITouch的方法@property(nonatomic,readonly)UIEventType type;
@property(nonatomic,readonly)UIEventSubtype subtype;
@property(nonatomic,readonly)NSTimeInterval timestamp;
#import "ViewController.h"#import "CZPointView.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet CZPointView *paintView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}// 回退按钮的单击事件- (IBAction)backBtnClick { [self.paintView back]; }// 清屏按钮的单击事件- (IBAction)clearScreen:(id)sender { [self.paintView clear];}// 保存按钮的单击事件- (IBAction)saveBtnClick:(id)sender { [self.paintView save];}// 设置线条颜色的按钮的单击事件- (IBAction)setColor:(UIButton *)sender { self.paintView.currentColor = sender.backgroundColor;}
CZPointView.h
#import <UIKit/UIKit.h>@interface CZPointView : UIView// 保存当前颜色@property (nonatomic, strong) UIColor *currentColor;// 返回一步- (void)back;// 清屏- (void)clear;// 保存- (void)save;@end
@interface CZPointView ()// 保存 “所有线”的点@property (nonatomic, strong) NSMutableArray *pointOfAllLine;// 保存线的颜色@property (nonatomic, strong) NSMutableArray *colorOfLine;@end@implementation CZPointView// 懒加载- (NSMutableArray *)pointOfAllLine{ if (!_pointOfAllLine) { _pointOfAllLine = [NSMutableArray array]; } return _pointOfAllLine;}- (NSMutableArray *)colorOfLine{ if (!_colorOfLine) { _colorOfLine = [NSMutableArray array]; } return _colorOfLine;}- (void)drawRect:(CGRect)rect{ // 创建一个图层上下文 CGContextRef layerContext = UIGraphicsGetCurrentContext(); // 设置线宽 CGContextSetLineWidth(layerContext, 2); // 设置线首尾样式与连接点的样式 CGContextSetLineCap(layerContext, kCGLineCapRound); CGContextSetLineJoin(layerContext, kCGLineJoinRound); // 遍历每条线 NSInteger lineCount = self.pointOfAllLine.count; for (NSInteger i = 0; i < lineCount; i++) { // 取出当前第一条线的数组 NSArray *pointsOfALine = self.pointOfAllLine[i]; // 设置线的颜色 UIColor *lineColor = self.colorOfLine[i]; [lineColor set]; // 取出线的点进行绘制 NSInteger pointsCount = pointsOfALine.count; for (NSInteger j = 0; j < pointsCount; j++) { CGPoint point = [pointsOfALine[j] CGPointValue]; if (j == 0) { CGContextMoveToPoint(layerContext, point.x, point.y); }else{ CGContextAddLineToPoint(layerContext, point.x, point.y); } } // 渲染 CGContextStrokePath(layerContext); } }- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ // 创建一个数组用来存放一条线的点 NSMutableArray *pointOfALine = [NSMutableArray array]; // 将这条线添加到所有线的数组中 [self.pointOfAllLine addObject:pointOfALine]; // 保存当前 “一条线”的颜色 if (!self.currentColor) { // 如果为空就给个默认颜色:黑色 [self.colorOfLine addObject:[UIColor blackColor]]; }else{ [self.colorOfLine addObject:self.currentColor]; } }- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ // 获取当前的UITouch对象 UITouch *touch = [touches anyObject]; // 获取当前手势的位置 CGPoint touchPoint = [touch locationInView:touch.view]; // 将获取的位置添加到 “一条线” 数组中 // 获取当前线的数组 NSMutableArray *pointsOfLine = [self.pointOfAllLine lastObject]; [pointsOfLine addObject:[NSValue valueWithCGPoint:touchPoint]]; // 根据触摸的点进行绘制 [self setNeedsDisplay];}// 清空- (void)clear{ // 删除所有的线与颜色 [self.pointOfAllLine removeAllObjects]; [self.colorOfLine removeAllObjects]; // 重新绘制 [self setNeedsDisplay]; }// 返回上一步- (void)back{ // 删除最后一条线 [self.pointOfAllLine removeLastObject]; // 重新绘制 [self setNeedsDisplay];}// 保存- (void)save{ // 开启位图上下文 UIGraphicsBeginImageContext(self.bounds.size); // 把当前view画在位图上下文 [self.layer renderInContext:UIGraphicsGetCurrentContext()]; // 获取图片 UIImage *captureImage = UIGraphicsGetImageFromCurrentImageContext(); // 结束编辑 UIGraphicsEndImageContext(); // 保存图片到沙盒中 // 获取document目录 NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; // 拼接完整路径 NSString *path = [document stringByAppendingPathComponent:@"capture.png"]; // 将图片保存为NSData类型 NSData *imageData = UIImagePNGRepresentation(captureImage); // 写入路径 [imageData writeToFile:path atomically:YES]; }@end
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- csu 1538 shop
- 埃尔朗根纲领(1872)的中译本(2010-01-03 21:53:27)
- 动态网站开发技术学习1:走进ASP.NET
- OpenSessionInView的优缺点
- Opencv--特征点检测算法对比
- 触摸事件
- Python 中文编码
- 嵌入式学习之,uboot的裁剪移植
- LightOJ 1025 - The Specials Menu (区间dp)
- 爬爬爬之路:OC语言(六) Block语法简介
- include指令和动作元素
- JAVA三星题之playing a TicTacToe game
- 第三部分 ARM裸机系列之教你学会刷操作系统
- Java 中泛型的全面解析