您的位置:首页 > 移动开发 > IOS开发

IOS学习 触摸和手势 手势识别器:单击、双击、轻扫、滑动、长按、旋转

2016-03-15 14:12 543 查看
@implementation HomeViewController

- (void)viewDidLoad {

[super
viewDidLoad];

// Do any additional setup after loading the view.

//单击

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(tap:)];

[self.view
addGestureRecognizer:tapGesture];

//双击

UITapGestureRecognizer *doubletap = [[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(doubletap:)];

doubletap.numberOfTapsRequired =
2 ;

[self.view
addGestureRecognizer:doubletap];

//区别单击、双击手势

[tapGesture requireGestureRecognizerToFail:doubletap];

//轻扫手势

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer
alloc]initWithTarget:self
action:@selector(swipe:)];

//指定轻扫方向,只能监听一个方向,默认向右轻扫

swipe.direction =
UISwipeGestureRecognizerDirectionUp;

/* UISwipeGestureRecognizerDirectionRight

UISwipeGestureRecognizerDirectionLeft

UISwipeGestureRecognizerDirectionUp

UISwipeGestureRecognizerDirectionDown */

[self.view
addGestureRecognizer:swipe];

//平移,滑动手势

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer
alloc]initWithTarget:self
action:@selector(panGesture:)];

[self.view
addGestureRecognizer:panGesture];

//长按手势

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer
alloc]initWithTarget:self
action:@selector(longPress:)];

//长按时间设置,秒

longPress.minimumPressDuration =
3;

[self.view
addGestureRecognizer:longPress];

//旋转手势

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer
alloc]initWithTarget:self
action:@selector(rotationTap:)];

[self.view
addGestureRecognizer: rotation];

}

//单击事件

-(void)tap:(UITapGestureRecognizer *)tapGesture{

NSLog(@"单击");

}

//双击事件

-(void)doubletap:(UITapGestureRecognizer *)tapGesture{

NSLog(@"双击");

}

//轻扫事件

-(void)swipe:(UISwipeGestureRecognizer *)swipe{

NSLog(@"轻扫");

}

//平移,滑动事件

-(void)panGesture:(UIPanGestureRecognizer *)panGesture{

NSLog(@"平移,滑动");

CGPoint point = [panGesture
locationInView:self.view];

NSLog(@"%@",NSStringFromCGPoint(point));

}

//长按事件

-(void)longPress:(UILongPressGestureRecognizer *)lp{

// NSLog(@"long press: %u",[lp state]); //手势状态

if (lp.state ==
UIGestureRecognizerStateEnded) {

return;

}

NSLog(@"长按");

}

//旋转事件

-(void)rotationTap:(UIRotationGestureRecognizer *)rotation{

NSLog(@"rotation :%f",rotation.rotation);
//旋转弧度

//转换成角度

float degree = rotation.rotation*(180/M_PI);

NSLog(@"degree = %f",degree);

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