我已经放置了很多代码来尝试让 UIImageView 进行缩放/平移。在下面的代码中,我有一个带有分页的大滚动视图,并且在每个页面中我都放入了一个滚动视图,其中包含一个我希望能够平移和缩放的 imageView。我已经阅读了很多文章,但仍然无法进行缩放和平移。也许专家可以帮助我弄清楚我错过了什么。我是 iOS/MonoTouch 开发的新手。
需要注意的一件事是,当我放置断点时,我的 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);