我正在使用 UIPinchGestureRecognizer 来触发模态 UIViewController 的外观,它允许缩放和平移图像。它本质上可以让您隔离一个图像并更详细地探索它。
新的 UIViewController 有自己的捏和平移手势识别器。
我注意到的一个缺点是,一旦新的 UIViewController 出现,用户必须在新的手势识别器识别触摸事件之前将手指从屏幕上移开并再次开始捏合。
理想情况下,我希望捏合是无缝的,因此一旦模态 UIViewController 出现,用户就可以继续捏合和/或平移。有没有办法将触摸事件从前一个视图控制器转换为模态事件,从而触发新 UIViewController 中的手势识别器?
我用来触发模态缩放视图控制器的代码:
- (IBAction)zoomImage:(UIPinchGestureRecognizer *)sender
{
// if the gesture was released while the scale factor is sufficiently big, show the modal view
if ( sender.state == UIGestureRecognizerStateEnded && sender.scale > 1.6f ) {
// prepare the modal view controller
ZoomViewController *viewControllerZoom = [[ZoomViewController alloc] initWithNibName:nil bundle:nil];
[viewControllerZoom setImage:self.imageViewImage.image andScale:sender.scale];
// present the modal view controller
[self presentViewController:viewControllerZoom animated:YES completion:nil];
// gracefully transition the image back to its original size
[UIView animateWithDuration:0.5f animations:^{
self.imageViewImage.transform = CGAffineTransformIdentity;
}];
}
else if ( sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled ) {
// revert to normal size on end
[UIView animateWithDuration:0.5f animations:^{
self.imageViewImage.transform = CGAffineTransformIdentity;
}];
}
else if ( sender.scale >= 1.0f ) {
// scale in place
CGFloat scale = sender.scale;
self.imageViewImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, scale, scale);
}
}