当前位置:编程学习 > wap >>

IOS开发(73)之放大和缩小的手势监听和处理

1 前言
UIPinchGestureRecognizer捕获并处理类似放大和缩小的手势动作。


2 代码实例
ZYViewController.m

 

[plain]
- (void)viewDidLoad 

    [super viewDidLoad]; 
    self.view.backgroundColor = [UIColor whiteColor]; 
    CGRect labelRect = CGRectMake(0.0f, /* X */ 
                                  0.0f, /* Y */ 
                                  200.0f, /* Width */ 
                                  200.0f); /* Height */ 
    self.myBlackLabel = [[UILabel alloc] initWithFrame:labelRect]; 
    self.myBlackLabel.center = self.view.center; 
    self.myBlackLabel.backgroundColor = [UIColor blackColor]; 
    /* 使UIPinchGestureRecognizer聚拉的手势的放大缩小功能生效 */ 
    self.myBlackLabel.userInteractionEnabled = YES; 
    [self.view addSubview:self.myBlackLabel]; 
    self.pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self 
                                                                    action:@selector(handlePinches:)]; 
    [self.myBlackLabel addGestureRecognizer:self.pinchGestureRecognizer]; 

 
- (void) handlePinches:(UIPinchGestureRecognizer*)paramSender{ 
    //UIPinchGestureRecognizer其中有两个比较重要的变量 scale 和 velocity,前者是一个比例范围,后者是一个变化速率的,也就是说每次变化的一个像素点。 
    if (paramSender.state == UIGestureRecognizerStateEnded){ 
        self.currentScale = paramSender.scale; 
    } else if (paramSender.state == UIGestureRecognizerStateBegan && self.currentScale != 0.0f){ 
    paramSender.scale = self.currentScale; 
    } 
    if (paramSender.scale != NAN && paramSender.scale != 0.0){ 
        paramSender.view.transform = CGAffineTransformMakeScale(paramSender.scale, 
                                                            paramSender.scale); 
    } 

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    CGRect labelRect = CGRectMake(0.0f, /* X */
                                  0.0f, /* Y */
                                  200.0f, /* Width */
                                  200.0f); /* Height */
    self.myBlackLabel = [[UILabel alloc] initWithFrame:labelRect];
    self.myBlackLabel.center = self.view.center;
    self.myBlackLabel.backgroundColor = [UIColor blackColor];
    /* 使UIPinchGestureRecognizer聚拉的手势的放大缩小功能生效 */
    self.myBlackLabel.userInteractionEnabled = YES;
    [self.view addSubview:self.myBlackLabel];
    self.pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self
                                                                    action:@selector(handlePinches:)];
    [self.myBlackLabel addGestureRecognizer:self.pinchGestureRecognizer];
}

- (void) handlePinches:(UIPinchGestureRecognizer*)paramSender{
    //UIPinchGestureRecognizer其中有两个比较重要的变量 scale 和 velocity,前者是一个比例范围,后者是一个变化速率的,也就是说每次变化的一个像素点。
    if (paramSender.state == UIGestureRecognizerStateEnded){
        self.currentScale = paramSender.scale;
    } else if (paramSender.state == UIGestureRecognizerStateBegan && self.currentScale != 0.0f){
    paramSender.scale = self.currentScale;
    }
    if (paramSender.scale != NAN && paramSender.scale != 0.0){
        paramSender.view.transform = CGAffineTransformMakeScale(paramSender.scale,
                                                            paramSender.scale);
    }
}
运行结果

 \
 


放大后结果

 

\

 

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,