我正在使用storyboard(iOS 6.0)为我的应用创建照片库查看器。这是我在storyboard中设置imageViewController的方式:
我确保在imageView和scrollView上启用userInteraction和多次触摸。我想要做的是,在捏时我想放大到imageView(最大比例3)并且能够平移。这是我目前所拥有的,但是,即使检测到捏合手势,比例也不会改变。
- (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;
}
}
大多数在线问题和教程都以编程方式创建视图并执行此操作,但代码越少越好(在我看来)。什么是让它与故事板一起使用的最佳方法?提前谢谢!!!
更新:
这是我的完整.m文件代码:
- (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
答案 0 :(得分:33)
我认为Apple文档中的更好解决方案
- (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;
}
答案 1 :(得分:2)
第一步是确保您的视图具有正确的委托实现。例如,在.m文件中
@interface myRootViewController () <.., UIGestureRecognizerDelegate, UIScrollViewDelegate, ...>
从文档中,确保已实现此功能:
UIScrollView类可以有一个必须采用UIScrollViewDelegate协议的委托。要使缩放和平移工作,代理必须实现 viewForZoomingInScrollView:和 scrollViewDidEndZooming:withView:atScale:;此外,最大(maximumZoomScale)和最小(minimumZoomScale)缩放比例必须不同。
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.fullScreenView;
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{}
scrollViewDidEndZooming方法现在可以保持为空。如果你已经这样做了,或者它仍然无法正常工作,请发布更多代码,然后更容易提供更具体的帮助。
答案 2 :(得分:1)
您需要执行Wadi上面建议的操作,但也要将setMinimumZoomScale设置为与setMaximumZoomScale不同。它们默认都是1.0f。
之后,UIImageView
应该是可追加的
答案 3 :(得分:0)
Here是苹果推荐做你想做的事情的方式。我使用了Apple提供的代码,它就像一个魅力!如果您想优化内存管理,可以使用iCarousel(由nicklockwood制作),它具有用于释放未使用视图的缓存管理!
答案 4 :(得分:0)
我创建了一个完全有效的演示应用程序(与Facebook的默认照片库类似),演示了嵌入在AutoLayout和故事板的滚动视图中的图像视图的缩放。在此处查看我的项目:http://rexstjohn.com/facebook-like-ios-photo-modal-gallery-swipe-gestures/。
它还包括通过MKNetworkKit加载异步照片和通过照片库轻触手势。享受,我希望它能节省每个人的时间,因为它有点烦人。
答案 5 :(得分:0)
我创建了一个类来处理类似于Instagram的UIImageViews的缩放。可能是你正在寻找的东西,否则你可以看看我是如何实现它的。 https://github.com/twomedia/TMImageZoom