UIImageView をズーム/パンするために、多くのコードを配置しました。以下のコードでは、ページング付きの大きな scrollView があり、各ページに、パンとズームを可能にしたい imageView を含む scrollView を配置しています。多くの記事を読みましたが、まだズームとパンが機能しません。たぶん、専門家が私が見逃しているものを理解するのを手伝ってくれるかもしれません. iOS/MonoTouch 開発は初めてです。
注意すべきことの 1 つは、ブレークポイントを配置したときに、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);