I have put a lot of code in place to try to get a UIImageView to Zoom/Pan. In the code below I have a large scrollView with paging and in each page I put in a scrollView which contains an imageView that I want to be able to Pan and Zoom. I've read many articles and still can't get the zooming and panning to work. Maybe an expert could help me figure out what I am missing. I am new to iOS/MonoTouch development.
我已经放了很多代码来尝试让UIImageView进行缩放/平移。在下面的代码中,我有一个带分页的大型scrollView,在每个页面中我放入一个scrollView,其中包含一个我希望能够进行平移和缩放的imageView。我已经阅读了很多文章,仍然无法进行缩放和平移工作。也许专家可以帮我弄清楚我错过了什么。我是iOS / MonoTouch开发的新手。
One thing to note is that my DidZoom (scrollViewDidZoom) delegate is not being hit when I put a breakpoint. ZoomingStarted is being hit, but it's not the delegate I need.
需要注意的一点是,当我放置断点时,我的DidZoom(scrollViewDidZoom)委托没有被击中。 ZoomingStarted正在被击中,但它不是我需要的代表。
//TODO: Code to put here to load image or whateer you want to display on this panel/page
//Each page will have it's own scrollview for scrolling and zooming that image on the page
var panelScrollView = new UIScrollView();
panelScrollView.CanCancelContentTouches=false;
panelScrollView.ClipsToBounds = true;
panelScrollView.MinimumZoomScale = 1.0f;
panelScrollView.MaximumZoomScale = 3.0f;
panelScrollView.MultipleTouchEnabled = true;
//panelScrollView.Delegate = this;
panelScrollView.BackgroundColor=UIColor.Black;
if (page == 1)
{
panelScrollView.BackgroundColor=UIColor.Red;
}
panelScrollView.ScrollEnabled = true;
panelScrollView.UserInteractionEnabled=true;
var ui = new UIImage (_assetImages[page], 1.0f, UIImageOrientation.Up);
//UIImageView imgView = new UIImageView(new RectangleF(100,100,1000,1000));
//imgView.Image = ui;
UIImageView imgView = new UIImageView(ui);
panelScrollView.ContentSize = new SizeF(imgView.Frame.Size.Width, imgView.Frame.Size.Height);
//Position the panelScrollView in the right page on the main scrollview
RectangleF frame = scrollView.Frame;
PointF location = new PointF();
location.X = frame.Width * (_numberOfPanels - 1);
frame.Location = location;
panelScrollView.Frame = frame;
//panelScrollView.ContentSize = new SizeF(1000,1000);
panelScrollView.BackgroundColor=UIColor.Green;
/*imgView.Frame = new RectangleF(panelScrollView.Frame.Width /2,
panelScrollView.Frame.Height/2,
imgView.Frame.Width,
imgView.Frame.Height);*/
imgView.Center = new PointF(panelScrollView.Frame.Width /2,
panelScrollView.Frame.Height/2);
imgView.ContentMode=UIViewContentMode.ScaleAspectFit;
//panelScrollView.ContentSize = panelScrollView.Bounds.Size;
panelScrollView.ZoomingStarted += (object sender, UIScrollViewZoomingEventArgs e) =>
{
int x;
};
panelScrollView.DidZoom += (object sender, EventArgs e) => {
//ScrollViewDidZoom handler
//handle zooming and positioning of the panel scroll view (aka, scrollview of the image)
var innerFrame = imgView.Frame;
var scrollerBounds = panelScrollView.Bounds;
if ( ( innerFrame.Size.Width < scrollerBounds.Size.Width ) || ( innerFrame.Size.Height < scrollerBounds.Size.Height ) )
{
var x = imgView.Center.X - ( scrollerBounds.Size.Width / 2 );
var y = imgView.Center.Y - ( scrollerBounds.Size.Height / 2 );
PointF myScrollViewOffset = new PointF(x, y);
panelScrollView.ContentOffset = myScrollViewOffset;
}
UIEdgeInsets anEdgeInset = new UIEdgeInsets(0, 0, 0, 0);
if ( scrollerBounds.Size.Width > innerFrame.Size.Width )
{
anEdgeInset.Left = (scrollerBounds.Size.Width - innerFrame.Size.Width) / 2;
anEdgeInset.Right = -anEdgeInset.Left; // I don't know why this needs to be negative, but that's what works
}
if ( scrollerBounds.Size.Height > innerFrame.Size.Height )
{
anEdgeInset.Top = (scrollerBounds.Size.Height - innerFrame.Size.Height) / 2;
anEdgeInset.Bottom = -anEdgeInset.Top; // I don't know why this needs to be negative, but that's what works
}
panelScrollView.ContentInset = anEdgeInset;
};
panelScrollView.AddSubview(imgView);
scrollView.AddSubview(panelScrollView);
1 个解决方案
#1
1
If you are getting zooming event by touching image itself then by default user interaction is disabled for a imageview. Set "imgView.userInteractionEnabled = YES" Or if the zooming event is coming from any delegate method, (in your case may be delegate of UIScrollView), then add a delegate listener like "panelScrollView.delegate = self"
如果您通过触摸图像本身获得缩放事件,则默认情况下,对于图像视图禁用用户交互。设置“imgView.userInteractionEnabled = YES”或者如果缩放事件来自任何委托方法,(在您的情况下可能是UIScrollView的委托),则添加委托侦听器,如“panelScrollView.delegate = self”
I think delegate implementation should be in following way.
我认为委托实施应该遵循以下方式。
Mention in code this line , panelScrollView.delegate = self; and add following methods in self class. If you get any warning for UIScrollViewDelegate is not defined then add UIScrollViewDelegate in your header file.
在代码中提到这一行,panelScrollView.delegate = self;并在自习课中添加以下方法。如果未收到UIScrollViewDelegate的任何警告,则在头文件中添加UIScrollViewDelegate。
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
}
After adding this code, delegate method should be get called.
添加此代码后,应调用委托方法。
#1
1
If you are getting zooming event by touching image itself then by default user interaction is disabled for a imageview. Set "imgView.userInteractionEnabled = YES" Or if the zooming event is coming from any delegate method, (in your case may be delegate of UIScrollView), then add a delegate listener like "panelScrollView.delegate = self"
如果您通过触摸图像本身获得缩放事件,则默认情况下,对于图像视图禁用用户交互。设置“imgView.userInteractionEnabled = YES”或者如果缩放事件来自任何委托方法,(在您的情况下可能是UIScrollView的委托),则添加委托侦听器,如“panelScrollView.delegate = self”
I think delegate implementation should be in following way.
我认为委托实施应该遵循以下方式。
Mention in code this line , panelScrollView.delegate = self; and add following methods in self class. If you get any warning for UIScrollViewDelegate is not defined then add UIScrollViewDelegate in your header file.
在代码中提到这一行,panelScrollView.delegate = self;并在自习课中添加以下方法。如果未收到UIScrollViewDelegate的任何警告,则在头文件中添加UIScrollViewDelegate。
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
}
After adding this code, delegate method should be get called.
添加此代码后,应调用委托方法。