44.手势识别器

来源:互联网 发布:java redis 取list 编辑:程序博客网 时间:2024/05/23 13:08
1.单击手势:    // 1.创建手势识别器    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];    // 1.1设置手势识别器的属性    // 设置用户必须点击几次才能出发点击事件    tap.numberOfTapsRequired = 1;    // 设置用户必须两根手指同时点击才会促发事件    tap.numberOfTouchesRequired = 1;    // 2.添加手势识别器到view    [self.iconView addGestureRecognizer:tap];    // 3.监听手势识别器    [tap addTarget:self action:@selector(tapView)];
2.长按手势:    // 1.创建手势识别器    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];    // 1.1设置长按手势识别器的长按多长时间触发     longPress.minimumPressDuration = 1;    // 手指按下后事件响应之前允许手指移动的偏移位    longPress.allowableMovement = 50;    // 2.添加手势识别器到View    [self.customView addGestureRecognizer:longPress];    // 3.监听手势识别器    [longPress addTarget:self action:@selector(longPressView)];
3.轻扫手势:    // 向上    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] init];    // 设置轻扫的方向,默认从右向左(最多同时支持两个方向,用"|"隔开)    swipe.direction = UISwipeGestureRecognizerDirectionUp;    [self.customView addGestureRecognizer:swipe];    [swipe addTarget:self action:@selector(swipeView)];
4.旋转手势:    UIRotationGestureRecognizer *gesture = [[UIRotationGestureRecognizer alloc] init];    [self.iconView addGestureRecognizer:gesture];    [gesture addTarget:self action:@selector(rotationView:)];- (void)rotationView:(UIRotationGestureRecognizer *)gesture{//    在传入的transform基础上递增一个弧度    self.iconView.transform = CGAffineTransformRotate(self.iconView.transform, gesture.rotation);    // 将旋转的弧度清零(注意不是将图片旋转的弧度清零, 而是将当前手指旋转的弧度清零)    gesture.rotation = 0;// 如果理解不了 , 记住就OK}
5.缩放手势:    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] init];    [self.iconView addGestureRecognizer:pinch];    [pinch addTarget:self action:@selector(pinchView:)];- (void)pinchView:(UIPinchGestureRecognizer *)pinch{    self.iconView.transform = CGAffineTransformScale(self.iconView.transform, pinch.scale, pinch.scale);    pinch.scale = 1.0;//和旋转手势的原理一样}
6.拖拽手势:    UIPanGestureRecognizer  *pan = [[UIPanGestureRecognizer alloc] init];    [self.customView addGestureRecognizer:pan];    [pan addTarget:self action:@selector(panView:)];- (void)panView:(UIPanGestureRecognizer *)pan{    // 返回的值是以手指按下的点为原点    CGPoint point = [pan translationInView:pan.view];    NSLog(@"拖拽事件 %@", NSStringFromCGPoint(point));    CGPoint temp = self.customView.center;    temp.x += point.x;    temp.y += point.y;    self.customView.center = temp;    // 理解不了就记住就OK    [pan setTranslation:CGPointZero inView:pan.view];}
7.若要一个view同时支持多个手势,需要设置手势的代理并实现以下代理方法(返回是否同时支持多个手势):// 该方法返回的BOOL值决定了view是否能够同时响应多个手势- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{    NSLog(@"%@ - %@", gestureRecognizer.class, otherGestureRecognizer.class);    return YES;}
0 0
原创粉丝点击