多点触摸案例

Posted on 2016-07-04 23:46  柠檬片  阅读(86)  评论(0)    收藏  举报
 1 多点触摸(案例)
 2 - 思路:
 3 0> 把控制器 view的背景色变成黑色
 4 1> 重写控制器的 touchesBegan 方法和 touchesMoved 方法
 5 2> 在控制器中添加 images 属性, 在该属性中保存2张 UIImage 图片
 6 3> 在 touchesBegan 方法中,:
 7 * 获取每一个触摸对象, 以及每个触摸对象的 location
 8 * 对于每个触摸对象创建一个 UIImageView, 并向其中添加一个 UIImage对象
 9 * 最后把这个 UIImageView 添加到 self.view 中
10 4> 在 touchesMoved 方法中执行与 touchesBegan 方法中同样的代码
11 5> 在 touchesEnded 方法中输出当前 self.view 中的 UIImageView 的个数
12 6> 在每次添加完毕一个 UIImageView 后, 执行一个 alpha = 0的动画, 动画执行完毕从 self.view 中移除这个控件
13 ** 注意: 一般默认情况下, 控件都不支持多点触摸(为了提高性能), 所以需要手动设置一个 UIView 允许多点触摸
14 
15 /** 参考代码:
16 
17 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
18 {
19     int i = 0;
20     
21     for (UITouch *touch in touches) {
22         
23         CGPoint loc = [touch locationInView:self.view];
24         UIImageView *imgView = [[UIImageView alloc] initWithImage:self.images[i]];
25         imgView.center = loc;
26         [self.view addSubview:imgView];
27         
28         i++;
29         
30         
31         // 慢慢消失
32         [UIView animateWithDuration:2.0 animations:^{
33             imgView.alpha = 0;
34         } completion:^(BOOL finished) {
35             [imgView removeFromSuperview];
36         }];
37     }
38 }
39 
40 
41 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
42 {
43     int i = 0;
44     
45     for (UITouch *touch in touches) {
46         
47         CGPoint loc = [touch locationInView:self.view];
48         UIImageView *imgView = [[UIImageView alloc] initWithImage:self.images[i]];
49         imgView.center = loc;
50         [self.view addSubview:imgView];
51         
52         i++;
53         
54         
55         // 慢慢消失
56         [UIView animateWithDuration:2.0 animations:^{
57             imgView.alpha = 0;
58         } completion:^(BOOL finished) {
59             [imgView removeFromSuperview];
60         }];
61     }
62 }
63 
64 
65 */
多点触摸