Zooming a UIScrollView in MonoTouch

2020-02-26 14:03发布

问题:

I'm trying to implement zooming functionality using a UIScrollView in MonoTouch but I just can't seem to make it work.

I've looked at a few tutorials which talk about setting a Delegate but most of it is in Obj-C which I can't translate over to C#.

Basically, I have a UIScrollView in Interface Builder that has an UIImageView as a Subview.

I've set the ContentSize of the UIScrollView to be the full size of the image and have also set a min/max zoom scale. Here's my code:

UIImage imgMap = new UIImage("img/map.png");
ivMap.Image = imgMap;
svMap.MinimumZoomScale = 1.0f;
svMap.MaximumZoomScale = 3.0f;
svMap.ContentSize = new System.Drawing.SizeF(ivMap.Frame.Width, ivMap.Frame.Height);

Any ideas on what I'm missing?

EDIT

I've created a new .xib file with the code provided by @Geoff but still no luck, the image just scrolls but still doesn't zoom.

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();

    UIScrollView sv = new UIScrollView (thisView.Frame);
    UIImageView iv = new UIImageView(UIImage.FromFile ("img/map.png"));

    sv.AddSubview (iv);
    sv.ContentSize = iv.Frame.Size;
    sv.MinimumZoomScale = 1.0f;
    sv.MaximumZoomScale = 3.0f;

    vwMineral.AddSubview(sv);
}

回答1:

Try:

var sv = new UIScrollView (window.Frame);
var iv = new UIImageView (UIImage.FromFile ("pat.jpg"));

sv.AddSubview (iv);
sv.ContentSize = iv.Frame.Size;
sv.MinimumZoomScale = 1.0f;
sv.MaximumZoomScale = 3.0f;
sv.MultipleTouchEnabled = true;
sv.ViewForZoomingInScrollView = delegate(UIScrollView scrollView) {
    return iv;
};      

window.AddSubview (sv);
window.MakeKeyAndVisible ();