Xamarin ios - 如何在UIScrollView中放入大图像

时间:2016-01-10 16:55:05

标签: c# uiscrollview xamarin.ios

我正在使用在C#中实现的Xamarin.iOS。我有一个图像列表。我正在尝试为Facebook或任何其他照片查看应用程序实现类似的功能。图像非常大但我想缩放它们以便它们总是适合滚动视图的边界 - 在这种情况下,scrollview符合屏幕边界。

我的挑战是:

  1. 我的图片填满整个屏幕但未正确缩放。
  2. 当我缩小时,我希望最小缩放比例返回到原始视图。
  3. 以下是我目前的代码:

    public override void ViewDidLoad()
        {
            base.ViewDidLoad ();
    
            //Setting up the scrollView  
    
            ScrollViewMain.BouncesZoom = false;
            ScrollViewMain.ClipsToBounds = true;
            index = 0;
    
            AddImageInScrollView(index);
    
            btnClose.AddTarget (this, new Selector ("MySelector"), UIControlEvent.TouchDown);
    
            btnPrevious.TouchUpInside += (object sender, EventArgs e) => {
                index--;
                if(index < 0)
                    index = 0;
    
                imageView.RemoveFromSuperview();
                AddImageInScrollView(index);
    
            } ;
    
            btnNext.TouchUpInside += (object sender, EventArgs e) => {
                index++;
                if((nuint)index > Car.allThumbnails.Count-1)
                    index = (int)Car.allThumbnails.Count-1;
    
                imageView.RemoveFromSuperview();
                AddImageInScrollView(index);
    
            } ;
    
    
            var tapGestureRecognizer = new UITapGestureRecognizer (OnDoubleTap);
                tapGestureRecognizer.NumberOfTapsRequired = 2;
    
            ScrollViewMain.AddGestureRecognizer (tapGestureRecognizer);
    }
    
    public void AddImageInScrollView(int index)
        {
    
            //Setting up the imageView
            imageView = new UIImageView(Car.allThumbnails.GetItem<UIImage>((nuint)index));
            imageView.UserInteractionEnabled = true;
            imageView.ContentMode = UIViewContentMode.ScaleAspectFit;
    
    
            //Adding the imageView to the scrollView as subView
            ScrollViewMain.AddSubview(imageView);
    
            ScrollViewMain.DecelerationRate = UIScrollView.DecelerationRateFast;
    
            var scrollViewFrame = ScrollViewMain.Frame;
            var scaleWidth = scrollViewFrame.Size.Width / ScrollViewMain.ContentSize.Width;
            var scaleHeight = scrollViewFrame.Size.Height / ScrollViewMain.ContentSize.Height;
    
            ScrollViewMain.ContentSize = new System.Drawing.SizeF((float)ScrollViewMain.Bounds.Size.Width, (float)ScrollViewMain.Bounds.Size.Height);
    
    
            var minScale = Math.Min(scaleWidth, scaleHeight);
    //Set the zoom properties
            ScrollViewMain.MaximumZoomScale = 3f;
            ScrollViewMain.MinimumZoomScale = (float)minScale;
    
            ScrollViewMain.ViewForZoomingInScrollView += (UIScrollView sv) => {
                return imageView;
    
            };
            ScrollViewMain.ZoomScale = 1.0f;
            ScrollViewMain.ContentMode = UIViewContentMode.ScaleAspectFit;
            imageView.ContentMode = UIViewContentMode.ScaleAspectFit;
            imageView.SizeToFit();
            ScrollViewMain.ContentSize = new System.Drawing.SizeF((float)imageView.Frame.Size.Width, (float)imageView.Frame.Size.Height);
    
    }
    

    这就是它的样子

    Image In ScrollView

0 个答案:

没有答案