IOS UIPinchGestureRecognizer 捏合手势


- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.title = @"";

self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];

NSURLRequest *request =[NSURLRequest requestWithURL:self.url

cachePolicy:NSURLRequestReloadIgnoringLocalCacheData

timeoutInterval:5.0];

[self.webView loadRequest:request];

[self.view addSubview:self.webView];

UIPinchGestureRecognizer* pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];

pinchGR.delegate = self; //

[_webView addGestureRecognizer:pinchGR];

}

//捏合手势的回调方法

- (void)pinchAction:(UIPinchGestureRecognizer*)recognizer{

if (recognizer.state==UIGestureRecognizerStateBegan || recognizer.state==UIGestureRecognizerStateChanged)

{

UIView *view=[recognizer view];

//扩大、缩小倍数

view.transform=CGAffineTransformScale(view.transform, recognizer.scale, recognizer.scale);

recognizer.scale=1;

}

//    //通过捏合手势的到缩放比率

//    float scale = sender.scale;

//    //得到该手势所作用的视图

//    UIView *view = sender.view;

//    //2D仿射变换函数中的缩放函数来实现视图的放大缩小

//

//    //是在原有基础上来改变当前的视图

//    //函数的第一个参数:现有的视图的transform值

//    //第二个参数:x轴上的缩放比率

//    //第三个参数:y轴上的缩放比率

//

//    //是在视图最初的transform状态上改变,不管执行多少次,都是以该视图最初的transform状态为基础来改变

//    view.transform = CGAffineTransformMakeScale(1, 10);

//    //    view.transform = CGAffineTransformScale(view.transform, scale, scale);

//    //每次捏合动作完毕之后,让此捏合值复原,使得它每次都是从100%开始缩放

//    sender.scale = 1;

}

// 允许多个手势并发

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

return YES;

}

你可能感兴趣的:(IOS UIPinchGestureRecognizer 捏合手势)