Xamarin.iOS滑动视图上图片(视图)的缩放(双击手势实现缩放)

滑动视图上实现图片(视图)的缩放功能:用到两个滑动视图
UIView pageView = new UIView();
UIScrollView contentScrollView = new UIScrollView();
contentScrollView.ShowsVerticalScrollIndicator = false; contentScrollView.ShowsHorizontalScrollIndicator = false;
contentScrollView.MinimumZoomScale = 0.2f;
contentScrollView.MaximumZoomScale = 3.0f;
contentScrollView.ZoomScale = 1.0f;
contentScrollView.Frame = new CGRect(0 , 0, View.Bounds.Width, View.Bounds.Height);
contentScrollView.ContentSize = new CGSize(View.Bounds.Width, View.Bounds.Height);
contentScrollView.ViewForZoomingInScrollView = delegate (UIScrollView scroll)
{
return pageView;
};

双击手势:实现缩放
public void HandleDoubleTapGesture(UIGestureRecognizer gesture)
{
var scroll = gesture.View.Superview as UIScrollView;
if(scroll != null)
{
var newScale = scroll.ZoomScale * 1.5f;
var zoomRect = zoomRectForScale(newScale, scroll, gesture.LocationInView(gesture.View));
scroll.ZoomToRect(zoomRect, true);
}
}

public CGRect zoomRectForScale(nfloat scale, UIScrollView scrollView, CGPoint center){
CGRect zoomRect = new CGRect() ;
zoomRect.Height = (scrollView.Frame.Height / scale);
zoomRect.Width = (scrollView.Frame.Width / scale);

zoomRect.X = (nfloat)(center.X - (zoomRect.Width / 2.0));
zoomRect.Y = (nfloat)(center.Y - (zoomRect.Height / 2.0));
return zoomRect;
}

你可能感兴趣的:(Xamarin.iOS)