给UIImageView添加各种手势

//先创建图片

 UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView.image = [UIImage imageNamed:@"10_0.jpg"];
    
    imageView.tag = 1;

    //添加滑动手势
   [imageView addGestureRecognizer:sgr];
    
    imageView.userInteractionEnabled=YES;
    
    
    UISwipeGestureRecognizer *rightsgr=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeClick:)];
    rightsgr.direction=UISwipeGestureRecognizerDirectionRight;
    
    [imageView addGestureRecognizer:rightsgr];
    
    
    
    [self.view addSubview:imageView];
}


-(void)swipeClick:(UISwipeGestureRecognizer*)sgr
{ //四个手势监听四个方向,四个手势可以都响应这个方法,然后在这个方法里判断手势的方向。
    if (sgr.direction & UISwipeGestureRecognizerDirectionRight)
        NSLog(@"向右滑动了");
    else if (sgr.direction & UISwipeGestureRecognizerDirectionLeft)
        NSLog(@"向左滑动了");
    else if (sgr.direction & UISwipeGestureRecognizerDirectionUp)
        NSLog(@"向上滑动了");
    else if (sgr.direction & UISwipeGestureRecognizerDirectionDown)
        NSLog(@"向下滑动了");}
//添加点击手势 为图片添加点击手势很方便的

 UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView.image = [UIImage imageNamed:@"10_0.jpg"];
    
    imageView.tag = 1;
   
    //给imageView添加敲击手势
    UITapGestureRecognizer *tgr=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
     //这个事件表示,发生敲击,响应[target action];
    [imageView addGestureRecognizer:tgr];
    
    imageView.userInteractionEnabled=YES;
    
    
    [self.view addSubview:imageView];
    
    //一个手势只能添加到一个视图上,但一个视图可以添加多个手势
    //添加双击手势
    UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapClick:)];
    
    [imageView addGestureRecognizer:doubleTap];
    
    doubleTap.numberOfTapsRequired=2;
    
    
    //设置双击失败,才响应单击
    [tgr requireGestureRecognizerToFail:doubleTap];
     //优先响应双击,确定不双击,才响应单击
}


-(void)tapClick:(UITapGestureRecognizer*)tgr
{

    //    获得拥有手势的视图 适合所有手势
   // UIView * view = tgr.view;
    NSLog(@"图片发生敲击事件");
    //可以在这里图片进行点击处理 不用再给图片添加点击方法
    
}

-(void)doubleTapClick:(UITapGestureRecognizer*)tgr
{
     NSLog(@"图片被双击了!你跟hello kitty有多大仇?");
    
}
//捏合手势 在图片的放大方面很有用的 想看高清图片时很有用
- (void)createImageView
{
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView.image = [UIImage imageNamed:@"10_0.jpg"];
    
    imageView.center=self.view.center;
    
    imageView.tag = 1;
    
    /*给imageView添加捏合手势*/
    UIPinchGestureRecognizer *pgr=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pich:)];
    
    [imageView addGestureRecognizer:pgr];

    
    imageView.userInteractionEnabled = YES;
    
    [self.view addSubview:imageView];

    
}

-(void)pich:(UIPinchGestureRecognizer*)pgr
{
    //用来保存我们每次捏合的结果
    static CGFloat scale = 1;
    
    pgr.view.transform=CGAffineTransformMakeScale(scale*pgr.scale, pgr.scale*scale);
    
    //scale是上次捏合结果比如50%,pgr.scale是本次手指捏合的比例如50%,最后结果25%
    //随着捏合,pgr.scale不断改变
    
    if (pgr.state==UIGestureRecognizerStateEnded) {
        
        scale*=pgr.scale;
    }
}
//还有摇动手势
- (void)createImageView
{
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    
    NSString *str=[[NSBundle mainBundle] pathForResource:@"10_3" ofType:@"jpg"];
    NSData *data=[NSData dataWithContentsOfFile:str];
    
    imageView.image=[UIImage imageWithData:data];
    
   // imageView.image = [UIImage imageNamed:@"10_3.jpg"];
    
    //启动动画
    [imageView shakeWithRaid:0.5 duration:0.5 repeatCount:0];
    //停止
    //    [imageView stopShake];
    
    [self.view addSubview:imageView];

}



转载于:https://my.oschina.net/u/2436241/blog/492573

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值