@interface HomeViewController : UIViewController
{
UIView *touchView;
UITouch *touch;
NSInteger *tapcount;
CGPoint point;
UIView *movieView;
}
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
touchView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 500)];
touchView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:touchView];
//是否支持多点触摸,默认为NO
touchView.multipleTouchEnabled = YES ;
//是否开启触摸事件,默认为开启
touchView.userInteractionEnabled = YES;
movieView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
movieView.backgroundColor = [UIColor redColor];
[touchView addSubview:movieView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"touchesBegan");
touch = [touches anyObject]; //获取点击内容
tapcount = touch.tapCount; //获取短时间内点击的次数
NSLog(@"tapCount = %d",tapcount);
//设定单双击事件
if (tapcount == 1) {
//延迟时间调用单击事件
[self performSelector:@selector(singleTap) withObject:nil afterDelay:0.5];
}else if(tapcount == 2){
//取消单击延迟事件
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
[self doubleTap];
}
//获取触摸点在视图上的坐标
point = [touch locationInView:touchView];
NSLog(@"%@",NSStringFromCGPoint(point));//将坐标转换成字符打印
}
- (void)singleTap{
NSLog(@"单击");
}
- (void)doubleTap{
NSLog(@"双击");
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"touchesMoved");
//重新获取
touch = [touches anyObject];
point = [touch locationInView:touchView];
CGRect frame = movieView.frame; //获取frame值
frame.origin = point; //将移动光标的坐标赋值给frame
movieView.frame = frame; //重新赋值给视图movieView
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"touchesEnded");
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"touchesCancelled");
}