使用UIScrollView以编程方式放大较大图片的小区域 [英] Programmatically zooming into small area of larger picture using UIScrollView

查看:46
本文介绍了使用UIScrollView以编程方式放大较大图片的小区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的代码以编程方式放大到较大图片的一小部分.稍后我将添加点击代码,但是现在我只想看看它是否有效.

I'm trying to get my code to programmatically zoom into a small area of the larger picture. I'll add the tap code later, but right now I just want to see it work.

这段代码中的zoomToRect绝对不做任何事情,我根本不明白为什么.当我构建它时,图像只是位于0,0的原点.

The zoomToRect in this code does absolutely nothing and I simply don't understand why. When I build it, the image just sits there at the 0,0 origin.

我尝试使用:

setContentOffset和scrollRectToVisible都可以正常工作-图像移动到指定的坐标.但是这些都不是我想要的,因为我需要移动和缩放图像,而不仅仅是移动它.

setContentOffset and scrollRectToVisible and both these work fine -the image moves to the specified coordinates. But neither of these is what I want, because I need to move and zoom the image, not just move it.

但是zoomToRect完全拒绝执行任何操作.现在,我已经阅读了约50页的示例和教程,但该死的东西行不通.我不知道为什么要拔头发.显然,我缺少一些真正的基础或重要的观点.

But zoomToRect utterly refuses to do anything. I've read about 50 pages of examples and tutorials on this now and not a damn thing works. I'm tearing my hair out not knowing why. Clearly I'm missing some really fundamental or important point.

UIImage *myFirstImage = [UIImage imageNamed:@"manga_page.jpg"];

UIImageView *myFirstImageView = [[UIImageView alloc] initWithImage:myFirstImage];

[myFirstImageView setFrame:CGRectMake(0, 0, myFirstImage.size.width, myFirstImage.size.height)];


UIScrollView *myFirstScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

[myFirstScrollView setContentSize:CGSizeMake(myFirstImage.size.width, myFirstImage.size.height)];

[myFirstScrollView addSubview:myFirstImageView];

[self.view addSubview:myFirstScrollView];

[myFirstScrollView zoomToRect:CGRectMake(300, 300, 300, 300) animated:YES];

推荐答案

问题是我尚未定义缩放委托-请在此处查看viewForZoomingInScrollView(页面底部)

The problem was that I hadn't defined the delegate for zooming - see viewForZoomingInScrollView (bottom of page) here:

https://developer.apple.com/library/ios/documentation/uikit/reference/uiscrollviewdelegate_protocol/Reference/UIScrollViewDelegate.html#//apple_ref/occ/intfm/UIScrollViewDelegate/viewForZoomingInScrollView

当我切换到Xamarin框架时,在Objective C中没有代码片段.但是,C#解决方案如下:

I don't have a code fragment in Objective C as I switched to the Xamarin framework. However the C# solution is as follows:

scrollView.ViewForZoomingInScrollView = delegate (UIScrollView sv) { return imageView; };

imageView是UIImageView,其中包含要滚动的图像.

Where imageView is the UIImageView containing the image to be scrolled around.

这篇关于使用UIScrollView以编程方式放大较大图片的小区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆