我正在使用情节提要 (iOS 6.0) 为我的应用创建照片库查看器.这就是我的 imageViewController 在情节提要中的设置方式:
I'm using storyboard (iOS 6.0) to create a photo gallery viewer for my app. This is how my imageViewController is set up in storyboard:
我已确保在 imageView 和 scrollView 上启用 userInteraction 和多次触摸.我想要做的是,在捏我想放大 imageView(最大比例 3)并能够平移.这是我目前拥有的,但是,即使检测到捏合手势,比例也不会改变.
I've made sure to enable userInteraction and multiple touches on both the imageView and scrollView. What I want to do is, on pinch I want to zoom into the imageView (maximum scale 3) and be able to pan around. This is what I currently have, however, even though the pinch gesture is detected the scale does not change.
- (IBAction)imagePinched:(id)sender {
if (pinchRecognizer.state == UIGestureRecognizerStateEnded || pinchRecognizer.state == UIGestureRecognizerStateChanged) {
NSLog(@"gesture.scale = %f", pinchRecognizer.scale);
CGFloat currentScale = self.fullScreenView.frame.size.width / self.fullScreenView.bounds.size.width;
CGFloat newScale = currentScale * pinchRecognizer.scale;
if (newScale < 1) {
newScale = 1;
}
if (newScale > 3) {
newScale = 3;
}
CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
self.fullScreenView.transform = transform;
pinchRecognizer.scale = 1;
}
}
大多数在线问题和教程都涉及以编程方式创建视图并执行此操作,但代码越少越好(在我看来).让它与故事板一起使用的最佳方法是什么?提前谢谢!!!
Most questions and tutorials online deal with programmatically creating the views and doing this, but the less code the better (in my eyes). What's the best way to get this to work with storyboard? Thank you in advance!!!
更新:
这是我完整的 .m 文件代码:
Here is my full .m file code:
- (void)viewDidLoad
{
[super viewDidLoad];
//Assign an image to this controller's imageView
fullScreenView.image = [UIImage imageNamed:imageString];
//Allows single and double tap to work
[singleTapRecognizer requireGestureRecognizerToFail: doubleTapRecognizer];
}
- (IBAction)imageTapped:(id)sender {
NSLog(@"Image Tapped.");
//On tap, fade out viewController like the twitter.app
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)imageDoubleTapped:(id)sender {
NSLog(@"Image Double Tapped.");
//On double tap zoom into imageView to fill in the screen.
[fullScreenView setContentMode:UIViewContentModeScaleAspectFill];
}
- (IBAction)imagePinched:(id)sender {
if (pinchRecognizer.state == UIGestureRecognizerStateEnded || pinchRecognizer.state == UIGestureRecognizerStateChanged) {
NSLog(@"gesture.scale = %f", pinchRecognizer.scale);
CGFloat currentScale = self.fullScreenView.frame.size.width / self.fullScreenView.bounds.size.width;
CGFloat newScale = currentScale * pinchRecognizer.scale;
if (newScale < 1) {
newScale = 1;
}
if (newScale > 3) {
newScale = 3;
}
CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
self.fullScreenView.transform = transform;
pinchRecognizer.scale = 1;
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.fullScreenView;
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我认为 Apple 文档中有更好的解决方案
I think better solution in Apple Documentation
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView.minimumZoomScale=0.5;
self.scrollView.maximumZoomScale=6.0;
self.scrollView.contentSize=CGSizeMake(1280, 960);
self.scrollView.delegate=self;
}
查看 Apple 文档
这篇关于在滚动视图内的 UIImageView 上捏缩放效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
iOS - 使用故事板和自动布局使 UIScrollView 居中iOS - Using storyboard and autolayout to center the UIScrollView(iOS - 使用故事板和自动布局使 UIScrollView 居中)
从 imageview 点击手势获取索引或标签值get index or tag value from imageview tap gesture(从 imageview 点击手势获取索引或标签值)
无论内容大小如何,UIScrollView 都不会滚动UIScrollView not scrolling regardless of large contentSize(无论内容大小如何,UIScrollView 都不会滚动)
清除分页 UIScrollView 中的自动旋转转换Clean autorotation transitions in a paging UIScrollView(清除分页 UIScrollView 中的自动旋转转换)
UIScrollView 使用自动布局缩放UIScrollView zooming with Auto Layout(UIScrollView 使用自动布局缩放)
如何从 UIView/UIScrollView 创建图像How to create an image from a UIView / UIScrollView(如何从 UIView/UIScrollView 创建图像)