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

UITouch触摸与手势

2015-08-19 08:29 453 查看
UITouch触摸与手势1. //延迟调用单击事件,以便双击时可取消单击调用
[selfperformSelector:@selector(singleTap) withObject:nilafterDelay:0.2];
2. //类方法取消单击调用方法,及时调用双击方法
[NSObjectcancelPreviousPerformRequestsWithTarget:selfselector:@selector(singleTap) object:nil];
3.self.clipsToBounds= YES//当子视图越界时,剪切子视图
几种常用的手势//创建手势视图
UIView *gestureView = [[UIViewalloc]initWithFrame:CGRectMake(10, 20, 300, 300)];
gestureView.backgroundColor = [UIColororangeColor];
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(100, 400, 100, 50)];
label.text = @"0";
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColorblueColor];
label.tag = 100;
[self.viewaddSubview:label];
[self.viewaddSubview:gestureView];

/******************点击手势*******************/
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap1Action:)];
//点击的数量---点击的次数
tap1.numberOfTapsRequired = 1;
//点击的个数-----手指的个数
tap1.numberOfTouchesRequired = 1;
[gestureView addGestureRecognizer:tap1];

//单手双击
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap2Action:)]; tap2.numberOfTouchesRequired = 2;//双手双击
tap2.numberOfTapsRequired = 2;
[gestureView addGestureRecognizer:tap2];
//如果tap2触发了.则tap1则失效-----双击时,单击失效
[tap1 requireGestureRecognizerToFail:tap2];

/******************轻扫手势***********************/
UISwipeGestureRecognizer *swip1 = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swip1Action:)];
[gestureView addGestureRecognizer:swip1];

/******************平移手势***********************/
UIPanGestureRecognizer *pan1 = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(pan1Action:)];
[gestureView addGestureRecognizer:pan1];

/******************长按手势***********************/
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPressAction:)];
[gestureView addGestureRecognizer:longPress];

/******************旋转手势***********************/
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationAction:)];
[gestureView addGestureRecognizer:rotation];

/******************捏合手势***********************/
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchAction:)];
[gestureView addGestureRecognizer:pinch];

}
/******************点击手势*******************/
//单击
- (void)tap1Action:(UITapGestureRecognizer *)tap1
{
NSLog(@"单击");
UILabel *label = (UILabel *)[self.viewviewWithTag:100];
label.text = @"单击";
}
//双击
- (void)tap2Action:(UITapGestureRecognizer *)tap2
{
NSLog(@"双击");
UILabel *label = (UILabel *)[self.viewviewWithTag:100];
label.text = @"双击";
}
/******************轻扫手势***********************/
- (void)swip1Action:(UISwipeGestureRecognizer *)swip1
{
// NSLog(@"轻扫");
UILabel *label = (UILabel *)[self.viewviewWithTag:100];
if (swip1.direction == UISwipeGestureRecognizerDirectionRight) {
label.text = @"向右轻扫";
}elseif (swip1.direction == UISwipeGestureRecognizerDirectionLeft){
label.text = @"向左轻扫";
}elseif (swip1.direction == UISwipeGestureRecognizerDirectionUp){
label.text = @"向上轻扫";
}elseif (swip1.direction == UISwipeGestureRecognizerDirectionDown){
label.text = @"向下轻扫";
}
}
/******************平移手势***********************/
- (void)pan1Action:(UIPanGestureRecognizer *)pan1
{
// UILabel *label = (UILabel *)[self.viewviewWithTag:100];
CGPoint p1 = [pan1 locationInView:pan1.view];
NSLog(@"平移的坐标是:%@",NSStringFromCGPoint(p1));
}
/******************长按手势***********************/
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
UILabel *label = (UILabel *)[self.viewviewWithTag:100];
if (longPress.state == UIGestureRecognizerStateBegan) {
label.text = @"开始长按";
}elseif (longPress.state == UIGestureRecognizerStateEnded){
label.text = @"长按结束";
}

}
/******************旋转手势***********************/
- (void)rotationAction:(UIRotationGestureRecognizer *)rota
{
// UILabel *label = (UILabel *)[self.viewviewWithTag:100];
//旋转的弧度
CGFloat r = rota.rotation;
NSLog(@"旋转的弧度是:%.2f",r);
NSLog(@"旋转的角度是:%.2f",180 * r / M_PI);

}
/******************捏合手势***********************/
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch

{
//获得捏合的比例
CGFloat scale = pinch.scale;
pinch.view.transform = CGAffineTransformMakeScale(scale, scale);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  触摸与手势