UIImageView 和手势识别器

1. UIImageView的初始化和设置图片

   //初始化
   UIImageView *imageView = [UIImageView alloc]initWithFrame:CGRectMake(50,50,50,50)];
   
   设置图片
   [imageView setImage:[UIImage imageNamed:@""]];//第一种针对是本地的单个图片
   
   [imageView setImage:[UIImae imageWithContentOfFile:[NSBundle mainBundle] pathForResource: ofType:]
   
   ]];
   
   //第三种
   NSData *data = [NSData dataWithContentOfFile:[NSBundle mainBundle] pathForResource: ofType:];
   
   UIImage *image = [UIImage imageWithData:data];
   
   [imageView setImage:image];//一般式请求网络上的数据。
   
2. 手势识别器

   手势识别器有7个子类,分别是轻拍手势、平移手势、轻扫手势、缩放手势、旋转手势、长按手势、屏幕边界平移手势。
   
   UITapGestureRecoginizer 是轻拍手势识别器,能识别轻拍操作。
   
   UILongPressGestureRecoginizer 是长按手势识别器,能识别长按操作
   
   UIRotationCestureRecoginizer 是旋转手势识别器,能识别旋转操作
   
   UIPitchGestureRecoginizer 是捏合手势识别器,能识别捏合操作
   
   UIPanGestureRecoginizer 是平移手势识别器,能识别拖拽操作
   
   UISwipeGestureRecoginizer 是清扫手势识别器,能识别拖拽操作
   
   UIScreenEdgePanGestureRecognizer 是屏幕边缘清扫识别器,是iOS7中新增加的一个

3.使用手势识别器的方法

  分为两步:创建手势实例,指定一个回调方法,当手势开始、改变、或结束时,回调方法被调用
  
  添加到view上,每个手势只对于一个View,但每个view可以对应对个手势

3.1 对图片进行拖拽的代码

    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
    
    imageView.frame = CGRectMake(50, 50, 100, 150);
   
    //创建手势实例平移
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector    (panAction:)];
   
    //添加到Imageview上
    [imageView addGestureRecognizer:pan];
    
    //创建手势缩放的
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
    
    [imageView addGestureRecognizer:pinch];
    
        //添加手势旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
    
    [imageView addGestureRecognizer:rotation]; 
           
    self.view.backgroundColor = [UIColor whiteColor];
    //打开交互,不然不会响应
    imageView.userInteractionEnabled = YES;
    
    [self.view addSubview:imageView];
  
  //回调的方法平移的
- (void)panAction:(UIPanGestureRecognizer *)recoginize
{
    CGPoint translation = [recoginize translationInView:self.view];
    
    recoginize.view.center = CGPointMake(recoginize.view.center.x + translation.x, recoginize.view.ce
    
    nter.y + translation.y);
    
    [recoginize setTranslation:CGPointZero inView:self.view];
}   

  //回调方法缩放
  
  - (void)pinchAction:(UIPinchGestureRecognizer *)recoginze
{
    recoginze.view.transform = CGAffineTransformScale(recoginze.view.transform, recoginze.scale, recoginze.scale);
    
    recoginze.scale = 1;
}  

 //旋转的手势的方法
- (void)rotationAction:(UIRotationGestureRecognizer *)recoginze
{
    recoginze.view.transform = CGAffineTransformRotate(recoginze.view.transform, recoginze.rotation);
    
    recoginze.rotation = 0;
}   
 
4. 手势之间是互斥的,如果你想同时出发多个的话,必须实现一个协议UIGestureRecognizerDelegate

    @interface ViewController : UIViewController<UIGestureRecognizerDelegate>  

    @end 
    
    并在协议里的方法返回YES
    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWi
    
    thGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer  

    {  
    
         return YES;  

    }
    
    把self作为代理设置给手势
   
    pan.delegate = self;  

    pinch.delegate = self;  
    
    rotation.delegate = self;

//

转载于:https://my.oschina.net/u/2322034/blog/403194

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值