zoukankan      html  css  js  c++  java
  • iOS手势之pinch

    今天用地图的时候有用到pinch 捏合手势

    通过捏合手势动作可以很轻松的来改变视图元素的一个比例

     手势的动作状态有如下三种,一般是按照顺序来进行转换的。 

    1. UIGestureRecognizerStateBegan 

    2. UIGestureRecognizerStateChanged 

    3. UIGestureRecognizerStateEnded 

    一旦捏合手势动作产生了之后,我们就需要在捕获的事件中进行一个页面调整。其中有两个比较重要的变量 scale 和 velocity ,前者是一个比例范围,后者是一个变化速率的,也就是说每次变化的一个像素点。 

    由于 scale 这个属性的值是每次都在变的,所以我们需要用另外一个变量来保存当前的一个scale的值,这个变量叫做currentScale,这样我们就可以进行一个缩小,变大的视图效果了 。

    代码:

    1. #import "ViewController.h"  
    2.   
    3. @interface ViewController ()  
    4. @property(nonatomic, strong)UIPinchGestureRecognizer *pinchGestureRecognizer;  
    5. @property(nonatomic, strong)UIView *myView;  
    6. @property(nonatomic, unsafe_unretained)CGFloat currentScale;  
    7. @end  
    8.   
    9. @implementation ViewController  
    10.   
    11. - (void)viewDidLoad  
    12. {  
    13.     [super viewDidLoad];  
    14.       
    15.     CGRect labelRect = CGRectMake(0, 0, 200, 200);  
    16.     self.myView= [[UIView alloc] initWithFrame:self.view.frame];  
    17.     self.myView.center = self.view.center;  
    18.     self.myView.backgroundColor = [UIColor grayColor];  
    19.     //打开view的交互  
    20.     self.myBlackLabel.userInteractionEnabled = YES;  
    21.     [self.view addSubview:self.myView];  
    22.       
    23.     //创建一个手势  
    24.     self.pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinches:)];  
    25.     [self.myView addGestureRecognizer:self.pinchGestureRecognizer];  
    26. }  
    27.   #pragma mark - 手势事件
    28. -(void)handlePinches:(UIPinchGestureRecognizer *)paramSender{  
    29.     if (paramSender.state == UIGestureRecognizerStateEnded) {  
    30.         self.currentScale = paramSender.scale;  
    31.     }else if(paramSender.state == UIGestureRecognizerStateBegan && self.currentScale != 0.0f){  
    32.         paramSender.scale = self.currentScale;  
    33.     }  
    34.     if (paramSender.scale !=NAN && paramSender.scale != 0.0) {  
    35.         paramSender.view.transform = CGAffineTransformMakeScale(paramSender.scale, paramSender.scale);  
    36.     }  
    37. }  
    38.   
    39. - (void)didReceiveMemoryWarning  
    40. {  
    41.     [super didReceiveMemoryWarning];  
    42.     // Dispose of any resources that can be recreated.  
    43. }  
    44.   
    45. @end
  • 相关阅读:
    iOS使用webView 加载网页,在模拟器中没有问题,而真机却白屏了。App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist f
    xcode快捷方式 一 快速找到对应文件
    cell 和 cellHeight的先后执行顺序
    tableView 选中cell时,获取到当前cell
    ios10 UNNtificationRequest UNUserNotificationCenter的应用 推送之本地推送
    下载老版本的Xcode
    敏捷开发
    关于tableView的错误提示
    iOS的内购
    PHP常用算法和数据结构示例
  • 原文地址:https://www.cnblogs.com/pengjuwang/p/5378830.html
Copyright © 2011-2022 走看看