iOS手势

iOS点击手势UITapGestureRecognizer :

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 150, 150)];
    imgview.image = [UIImage imageNamed:@"6.jpg"];
    //注意开启用户交互
    imgview.userInteractionEnabled = YES;
    [self.view addSubview:imgview];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageVIewtap:)];
//    tap.delegate = self;  //协议
    //点击次数  单击还是双击
    tap.numberOfTapsRequired = 2;
    //设置属性 一个手指头还是多个手指头点击
    tap.numberOfTouchesRequired = 1;
    //添加
    [imgview addGestureRecognizer:tap];
}
 //触发手势点击事件
-(void)imageVIewtap:(UIGestureRecognizer *)tap{

    self.view.backgroundColor=[UIColor greenColor];
}
        //默认为单击
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapClick:)];
        tap.delegate = self;
        
        [view addGestureRecognizer :tap];

iOS清扫手势 UISwipeGestureRecognizer

- (void)viewDidLoad
{
    [super viewDidLoad];

    _imgview=[[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 200, 200)];
    _imgview.image=[UIImage imageNamed:@"6.jpg"];
    _imgview.userInteractionEnabled=YES;
 
    UISwipeGestureRecognizer *pan = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(imageVIewtap:)];
    pan.delegate = self;
    pan.direction = UISwipeGestureRecognizerDirectionRight;
    //设置属性 不设置默认为1
    pan.numberOfTouchesRequired = 1;
    //添加手势到视图上
    [_imgview addGestureRecognizer:pan];
    [self.view addSubview:_imgview];
}

//判断手势移动方向  -上-下-左-右
-(void)imageVIewtap:(UISwipeGestureRecognizer *)tap{
    
    if (tap.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"left");
    }
    else if(tap.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"right");
    }
    /*
     UISwipeGestureRecognizerDirectionRight = 1 << 0,
     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
     UISwipeGestureRecognizerDirectionUp    = 1 << 2,
     UISwipeGestureRecognizerDirectionDown  = 1 << 3
    */
}
iOS移动手势 UIPanGestureRecognizer:
- (void)viewDidLoad
{
    [super viewDidLoad];
    _imgview = [[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 200, 200)];
    _imgview.image = [UIImage imageNamed:@"6.jpg"];
    _imgview.userInteractionEnabled = YES;
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(imageVIewtap:)];
    pan.delegate=self;
    
    //设置属性
    pan.minimumNumberOfTouches = 1;
    
    [_imgview addGestureRecognizer:pan];
    [self.view addSubview:_imgview];
}

-(void)imageVIewtap:(UIGestureRecognizer *)tap{
    
    self.view.backgroundColor=[UIColor greenColor];
    if (tap.state==UIGestureRecognizerStateBegan) {
        NSLog(@"开始移动");
        //缩放图片
        self.imgview.transform=CGAffineTransformMakeScale(0.5, 0.5);
    }
    else if (tap.state==UIGestureRecognizerStateChanged){
        NSLog(@"移动中");
        self.imgview.center=[tap locationInView:self.view];
    }
    else if(tap.state==UIGestureRecognizerStateEnded){
        NSLog(@"移动完成");
        self.imgview.transform=CGAffineTransformIdentity;
    }
}

iOS长按手势 UILongPressGestureRecognizer
- (void)viewDidLoad
{
    [super viewDidLoad];
    _imgview = [[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 200, 200)];
    _imgview.image = [UIImage imageNamed:@"6.jpg"];
    _imgview.userInteractionEnabled = YES;
    
    UILongPressGestureRecognizer *pan = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(imageVIewtap:)];
    pan.delegate=self;
    
    //设置属性
    pan.minimumPressDuration = 2;

    [_imgview addGestureRecognizer:pan];
    [self.view addSubview:_imgview];
}

-(void)imageVIewtap:(UILongPressGestureRecognizer *)tap{
    
    if (tap.state == UIGestureRecognizerStateEnded) {
        NSLog(@"长按手势完毕");
    }else{
        NSLog(@"识别了点击手势");
    }
}
iOS旋转手势 UIRotationGestureRecognizer
- (void)viewDidLoad
{
    [super viewDidLoad];
    _imgview = [[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 200, 200)];
    _imgview.image = [UIImage imageNamed:@"6.jpg"];
    _imgview.userInteractionEnabled = YES;
    
    UIRotationGestureRecognizer *pan = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(imageVIewtap:)];
    pan.delegate = self;

    [_imgview addGestureRecognizer:pan];
    [self.view addSubview:_imgview];
}

-(void)imageVIewtap:(UIRotationGestureRecognizer *)tap{
    
    tap.view.transform = CGAffineTransformRotate(tap.view.transform, tap.rotation);
    tap.rotation = 0.0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值