您的位置:首页 > 产品设计 > UI/UE

IOS学习 触摸和手势UITouch 单击双击、移动视图

2016-03-14 16:56 441 查看
@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");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: