我有一个ImageViewController,它呈现一个ImageZoomView(目前还没有缩放,但很快就会出现)。当我点击图像时,整个图像会略微向上移动。这不应该发生,图像应该留在它的位置,直到用户滑动或捏住。
ImageViewController位于UIPageViewController内部,允许在视图之间滑动。但是,我唯一的问题是使用ImageViewController(即VideoViewController和PdfViewController都工作正常,并且不会在水龙头上向上移动)
ImageZoomView是一个带有UIImageView的UIScrollView,因为它是子控件。
ImageZoomView:
public class ImageZoomView : UIScrollView
{
private UIImage _image;
private UIImageView _imageView;
public ImageZoomView()
{
_imageView = new UIImageView
{
ContentMode = UIViewContentMode.ScaleAspectFill
};
AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
ClipsToBounds = true;
AddSubview(_imageView);
}
public int Index { get; set; }
public override void LayoutSubviews()
{
base.LayoutSubviews();
var imageWidth = Bounds.Width;
_imageView.Frame = new CGRect(
Bounds.Left, Bounds.Top,
imageWidth, imageWidth / _image.Size.Width * _image.Size.Height);
ContentSize = _imageView.Bounds.Size;
}
public void DisplayImage(UIImage image)
{
_image = image;
_imageView.Image = image;
}
}
ImageViewController:
public class ImageViewController : AssetViewController
{
private ImageZoomView _imageView;
public ImageViewController(int index, Media media) : base(index, media) { }
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
_imageView.Frame = View.Bounds;
}
public override void LoadView()
{
_imageView = new ImageZoomView()
{
Index = Index,
BackgroundColor = UIColor.White,
};
_imageView.DisplayImage((_media as LocalMedia<UIImage>).Resource);
View = _imageView;
}
}
AssetViewController:
public abstract class AssetViewController : UIPageViewController
{
protected Media _media;
public AssetViewController(int index, Media media)
{
_media = media;
Index = index;
View.BackgroundColor = Globals.ColorDark;
}
public int Index { get; set; }
public Media CurrentMedia { get { return _media; } }
}
答案 0 :(得分:0)
引起问题的几件事
所有控制器都是从UIPageViewController派生的。内容控制器不应从UIPageViewController
在ImageViewController中,代码View = _imageView;
将主控制器视图替换为ScrollView。我建议使用AddSubview(_imageView);
我看到这个错误的动作不是滚动视图,而是以某种方式查看整个控制器。
经常调用LayoutSubviews并且一直计算同样的事情,因为图像大小和滚动边界都没有变化。
以下建议的改进。
ImageViewController是PageViewController内的内容控制器,ImageZoomView是ImageViewController里面的滚动视图
PageViewController
public partial class PageViewController : UIPageViewController
{
public PageViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
List<ImageViewController> pages = new List<ImageViewController>();
UIStoryboard board = UIStoryboard.FromName("Main", null);
for (int i = 0; i < 3; i++)
{
ImageViewController ctrl = (ImageViewController)board.InstantiateViewController("imageControllerStoryboardID");
ctrl.Index = i;
pages.Add(ctrl);
}
DataSource = new PageDataSource(pages);
View.Frame = View.Bounds;
//Set the initial content (first page)
SetViewControllers(new UIViewController[] { pages[0] }, UIPageViewControllerNavigationDirection.Forward, false, s => { });
}
}
public class PageDataSource : UIPageViewControllerDataSource
{
List<ImageViewController> pages;
public PageDataSource(List<ImageViewController> pages)
{
this.pages = pages;
}
override public UIViewController GetPreviousViewController(UIPageViewController pageViewController, UIViewController referenceViewController)
{
var currentPage = referenceViewController as ImageViewController;
if (currentPage.Index == 0)
{
return pages[pages.Count - 1];
}
else
{
return pages[currentPage.Index - 1];
}
}
override public UIViewController GetNextViewController(UIPageViewController pageViewController, UIViewController referenceViewController)
{
var currentPage = referenceViewController as ImageViewController;
return pages[(currentPage.Index + 1) % pages.Count];
}
}
ImageViewController
public partial class ImageViewController : UIViewController
{
public int Index;
private ImageZoomView _imageView;
public ImageViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
_imageView = new ImageZoomView(UIImage.FromFile("MergedImage.png"), View.Frame)
{
Index = Index,
BackgroundColor = UIColor.Blue,
};
View.AddSubview(_imageView);
}
}
ImageZoomView
public class ImageZoomView : UIScrollView
{
private UIImage _image;
private UIImageView _imageView;
public ImageZoomView(UIImage image, CGRect frame):base()
{
_imageView = new UIImageView
{
ContentMode = UIViewContentMode.ScaleAspectFill,
};
_image = image;
_imageView.Image = image;
Frame=_imageView.Frame = frame;
AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
ClipsToBounds = true;
AddSubview(_imageView);
ContentSize = new CGSize(Frame.Width, Frame.Width / _image.Size.Width * _image.Size.Height);
_imageView.Center = new CGPoint(ContentSize.Width / 2, ContentSize.Height / 2);
}
public int Index { get; set; }
}
原始样本中仍有一些代码作为未使用的Index字段和private _image变量。可能在将来有用,所以我保留了它们