设为首页 加入收藏

TOP

Core Text实现编辑的时候会用到的(一)
2014-11-23 21:45:50 】 浏览:7987
Tags:Core Text 实现 编辑 时候 用到
首先说一下实现的原理, 首先当手指开始触摸屏幕 以及滑动的时候, 效果与画矩形框是一样的 因此 此时的代码也机会没有区别,
当手指松开后 在当前的矩形框处创建一个临时的textView ,并且背景变为灰色,textView 编辑结束, 在textView的 完成委托方法中 去掉灰色的背景 去掉临时的textView 在相同的位置上 利用coreText 显示出刚才编辑的内容
首先 手指触摸 会调用到
TextTool.m
1
2
3
4
5
6
7
8
9
10
11
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UIView *touchedView = [delegate viewForUseWithTool:self];
//注意下面这句代码
[touchedView endEditing:YES];
// we only track one touch at a time for this tool.
UITouch *touch = [[event allTouches] anyObject];
// remember the touch, and its original start point, for future
[trackingTouches addObject:touch];
CGPoint location = [touch locationInView:touchedView];
[startPoints addObject:[NSValue valueWithCGPoint:location]];
}
代码 除了注释的那一句 基本上与前面的矩形 是相同的 纪录下来 UITouch 的实例 以及 开始的点
而注释那一句 得到的touchedView 就是dudelView 如果此处设置为yes 在textView 成为第一响应 弹出键盘后,我们可以直接触摸view 来取消TextView的第一响应状态
随着手指移动 不断调用到 touchMoved 但是 与前面矩形的实现一样 这里的touchMoved方法为空 画出临时矩形的实现代码在
drawTemporary 方法中
TextTool.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (void)drawTemporary {
if (self.completedPath) {
[delegate.strokeColor setStroke];
[self.completedPath stroke];
} else {
UIView *touchedView = [delegate viewForUseWithTool:self];
for (int i = 0; i<[trackingTouches count]; i++) {
UITouch *touch = [trackingTouches objectAtIndex:i];
CGPoint startPoint = [[startPoints objectAtIndex:i] CGPointValue];
CGPoint endPoint = [touch locationInView:touchedView];
CGRect rect = CGRectMake(startPoint.x, startPoint.y, endPoint.x - startPoint.x, endPoint.y - startPoint.y);
UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
[delegate.strokeColor setStroke];
[path stroke];
}
}
}
completedPath是一个UIBezierPath 的实例, 在touchEnded方法中 将最终矩形的路径 设置到其中, 所以在手指抬起来以前 都只会进入到else分支, 根据 起点与当前手指触摸的点 描出一个矩形来
当手指抬起来后 进入touchEnded方法,而主要的实现代码就在这里面
TextTool.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UIView *touchedView = [delegate viewForUseWithTool:self];
for (UITouch *touch in [event allTouches]) {
// make a rect from the start point to the current point
NSUInteger touchIndex = [trackingTouches indexOfObject:touch];
// only if we actually remember the start of this touch...
if (touchIndex != NSNotFound) {
CGPoint startPoint = [[startPoints objectAtIndex:touchIndex] CGPointValue];
CGPoint endPoint = [touch locationInView:touchedView];
[trackingTouches removeObjectAtIndex:touchIndex];
[startPoints removeObjectAtIndex:touchIndex];
// detect short taps that are too small to contain any text;
// these are probably accidents
if (distanceBetween(startPoint, endPoint) < 50.0) return;
CGRect rect = CGRectMake(startPoint.x, startPoin
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C常见问题之exit 下一篇C语言变参函数的编写

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目