iOS-UIGestureRecognizer(手势)

来源:互联网 发布:数据挖掘毕业论文题目 编辑:程序博客网 时间:2024/04/30 02:21

1、点击手势

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];    tap.delegate = self;    [self.imageView addGestureRecognizer:tap];
#pragma mark -#pragma mark - 点击手势实现的方法-(void)tapAction:(UITapGestureRecognizer *)tap{    NSLog(@"点击了");}

2、轻扫手势

 UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];    swip.direction = UISwipeGestureRecognizerDirectionUp;    [self.imageView addGestureRecognizer:swip];
#pragma mark -#pragma mark - 轻扫手势实现方法-(void)swipAction:(UISwipeGestureRecognizer *)swip{    NSLog(@"清扫了");}

3、长按手势

UILongPressGestureRecognizer *longPres = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];    [self.imageView addGestureRecognizer:longPres];
#pragma mark -#pragma mark - 长按手势实现方法//默认长按会有两次触发效果,即点击时和取消点击时都会调用实现的方法-(void)longAction:(UILongPressGestureRecognizer *)longPres{    //设置点击时处理    if (longPres.state == UIGestureRecognizerStateBegan) {          NSLog(@"长按了");    }}

4、捏合手势

UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinAction:)];    [self.imageView addGestureRecognizer:pin];
#pragma mark -#pragma mark - 捏合手势- (void)pinAction:(UIPinchGestureRecognizer *)pin{    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pin.scale, pin.scale);    //复位    pin.scale = 1;}

5、旋转手势

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];    rotation.delegate = self;    [self.imageView addGestureRecognizer:rotation];
#pragma mark -#pragma mark - 旋转手势-(void)rotationAction:(UIRotationGestureRecognizer *)rotaion{    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotaion.rotation);    //复位    rotaion.rotation = 0;}

6、拖拽手势

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];    [self.imageView addGestureRecognizer:pan];
#pragma mark -#pragma mark - 拖拽手势-(void)panAction:(UIPanGestureRecognizer *)pan{    //获取手势的移动,也是相对于最开始的位置    CGPoint transP = [pan translationInView:self.imageView];    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);    //复位    [pan setTranslation:CGPointZero inView:self.imageView];}

常用代理方法:

//是否允许开始点击- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{    return YES;}//是否同时支持多种手势- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{    return YES;}//设置点击的范围- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{    //获取当前的触摸点    CGPoint curp = [touch locationInView:self.imageView];    if (curp.x <= self.imageView.bounds.size.width*0.5) {        return NO;    }else{        return YES;    }    return YES;}

代理方法都是可选的,想通过代理方法实现手势的某个效果 ,就把该手势设置代理。

0 0
原创粉丝点击