需要帮助弄清楚为什么我的UIImageView不会缩放/平移 - 包含代码片段

时间:2013-01-25 13:42:22

标签: ios objective-c cocoa xamarin.ios

我已经放了很多代码来尝试让UIImageView进行缩放/平移。在下面的代码中,我有一个带分页的大型scrollView,在每个页面中我放入一个scrollView,其中包含一个我希望能够进行平移和缩放的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);

1 个答案:

答案 0 :(得分:1)

如果您通过触摸图像本身获得缩放事件,则默认情况下,对于图像视图禁用用户交互。设置“imgView.userInteractionEnabled = YES” 或者,如果缩放事件来自任何委托方法(在您的情况下可能是UIScrollView的委托),则添加委托侦听器,如“panelScrollView.delegate = self”

我认为委托实施应该遵循以下方式。

在代码中提到这一行,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
{
}

添加此代码后,应调用委托方法。