1

我已经放置了很多代码来尝试让 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);
4

1 回答 1

1

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

我认为委托实现应该采用以下方式。

在代码中提到这一行,panelScrollView.delegate = self; 并在 self 类中添加以下方法。如果您收到未定义 UIScrollViewDelegate 的任何警告,请在头文件中添加UIScrollViewDelegate 。

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
}

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

于 2013-01-25T13:55:38.823 回答