------视图控制器的.m中------------
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *gestureView = [[UIView alloc]initWithFrame:CGRectMake(20, 50, 335, 400)];
gestureView.backgroundColor = [UIColor purpleColor];
[self.view addSubview:gestureView];
/**********************点击手势***************************/
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap1Action:)];
//一个点击手势,只能识别一种手势,单击和双击是两种不同的手势
//点击次数
tap1.numberOfTapsRequired = 1;
//点击个数
tap1.numberOfTouchesRequired =1;
//往gestureView张添加手势
[gestureView addGestureRecognizer:tap1];
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap2Action:)];
tap2.numberOfTapsRequired = 2;
[gestureView addGestureRecognizer:tap2];
//如果参数中的手势触发了,则自身失效
[tap1 requireGestureRecognizerToFail:tap2];
/**********************轻扫手势**************************/
UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
//设置方向
swip.direction = UISwipeGestureRecognizerDirectionLeft;
[gestureView addGestureRecognizer:swip];
/**********************平移手势**************************/
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[gestureView addGestureRecognizer:pan];
/**********************长按手势**************************/
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
//设置长按的最短时间
longPress.minimumPressDuration = 2;
[gestureView addGestureRecognizer:longPress];
/**********************旋转手势**************************/
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[gestureView addGestureRecognizer:rotation];
/**********************捏合手势**************************/
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[gestureView addGestureRecognizer:pinch];
}
- (void)tap1Action:(UITapGestureRecognizer *)tap1{
NSLog(@"单击");
}
- (void)tap2Action:(UITapGestureRecognizer *)tap2{
NSLog(@"双击");
}
- (void)swipAction:(UISwipeGestureRecognizer *)swip{
if (swip.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"向左轻扫");
}
}
- (void)panAction:(UIPanGestureRecognizer *)pan{
CGPoint point = [pan locationInView:pan.view];
NSLog(@"%@",NSStringFromCGPoint(point));
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress{
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按开始");
}else if (longPress.state == UIGestureRecognizerStateEnded){
NSLog(@"长按结束");
}
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation{
//获取旋转的角度-----先得到弧度
CGFloat r = rotation.rotation;
//转成角度
//180/PI = x/r
NSLog(@"x is %.2f",180/M_PI*r);
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch{
//获得捏合的比例
CGFloat scale = pinch.scale;
pinch.view.transform = CGAffineTransformMakeScale(scale, scale);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。