#在蓝懿学习iOS的日子#day20
1、touch触发点点击屏幕
@property (nonatomic, strong)UIImageView *iv;
- (void)viewDidLoad {
[super viewDidLoad];
self.iv = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
self.iv .backgroundColor = [UIColor blackColor];
[self.view addSubview:self.iv];
}
//开始
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 把set转成array
// NSArray *arr = touches.allObjects;
//NSLog(@"开始");
//*********获取单个手指坐标
UITouch*t=[touches anyObject];
CGPoint p = [t locationInView:self.view];
self.iv.center = p;
//在控制台输出的NO.1
NSLog(@"开始 %f--%f",p.x,p.y);
//NO.2把中心点转为字符串
NSLog(@"开始 %@",NSStringFromCGPoint(p));
//用户点击的数量
NSLog(@"=====%ld",t.tapCount);
//*********获取两个
NSArray *arr = touches.allObjects;
if (arr.count==2) {
UITouch *t1 = arr[0];
UITouch *t2 = arr[1];
CGPoint p1 = [t1 locationInView:self.view];
CGPoint p2 = [t2 locationInView:self.view];
NSLog(@"p1=%@ p2=%@",NSStringFromCGPoint(p1),NSStringFromCGPoint(p2));
}
}
//移动
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"移动");
UITouch*t=[touches anyObject];
CGPoint p = [t locationInView:self.view];
self.iv.center = p;
NSLog(@"移动 %@",NSStringFromCGPoint(p));
}
//结束
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//NSLog(@"结束");
UITouch*t=[touches anyObject];
CGPoint p = [t locationInView:self.view];
self.iv.center = p;
NSLog(@"结束 %@",NSStringFromCGPoint(p));
}
//因为意外退出
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"因为意外退出");
[super viewDidLoad];
self.iv = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
self.iv .backgroundColor = [UIColor blackColor];
[self.view addSubview:self.iv];
}
//开始
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 把set转成array
// NSArray *arr = touches.allObjects;
//NSLog(@"开始");
//*********获取单个手指坐标
UITouch*t=[touches anyObject];
CGPoint p = [t locationInView:self.view];
self.iv.center = p;
//在控制台输出的NO.1
NSLog(@"开始 %f--%f",p.x,p.y);
//NO.2把中心点转为字符串
NSLog(@"开始 %@",NSStringFromCGPoint(p));
//用户点击的数量
NSLog(@"=====%ld",t.tapCount);
//*********获取两个
NSArray *arr = touches.allObjects;
if (arr.count==2) {
UITouch *t1 = arr[0];
UITouch *t2 = arr[1];
CGPoint p1 = [t1 locationInView:self.view];
CGPoint p2 = [t2 locationInView:self.view];
NSLog(@"p1=%@ p2=%@",NSStringFromCGPoint(p1),NSStringFromCGPoint(p2));
}
}
//移动
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"移动");
UITouch*t=[touches anyObject];
CGPoint p = [t locationInView:self.view];
self.iv.center = p;
NSLog(@"移动 %@",NSStringFromCGPoint(p));
}
//结束
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//NSLog(@"结束");
UITouch*t=[touches anyObject];
CGPoint p = [t locationInView:self.view];
self.iv.center = p;
NSLog(@"结束 %@",NSStringFromCGPoint(p));
}
//因为意外退出
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"因为意外退出");
}
2、 当用户使用超过一个的触屏点的坐标要勾选
3、超出视图的Bouns不显示