ScrollView缩放:图像平移无法按预期工作

时间:2015-11-16 08:46:58

标签: c# ios xamarin xamarin.ios

我正致力于更新旧服务的图像兼容性并遇到问题。

几个月前,我已经查找了如何缩放和平移图像。我在官方Xamarin网站(https://developer.xamarin.com/recipes/ios/content_controls/scroll_view/zoom_a_scrollview/)上找到了这样做的方法。当时这很好用,但现在它没有按预期工作。

以下是我正在运行的代码的片段:

修改图像的大小,使其宽度始终等于屏幕宽度,并使用屏幕宽度修改高度,以保留图像的各个方面。

var width = image.Size.Width;
var height = image.Size.Height;
var newWidth = (GlobalSupport.ScreenWidth - 30);
var newHeight = height * newWidth / width;

UIGraphics.BeginImageContext(new SizeF(newWidth, newHeight));
image.Draw(new RectangleF(0, 0, newWidth, newHeight));
image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();

创建imageView,将图像添加到其中,然后设置滚动视图及其缩放。

imageView = new UIImageView(
    new RectangleF((screenWidth - image.Size.Width) / 2, 
                   0, image.Size.Width, image.Size.Height));

imageView.Image = image;

scrollView = new UIScrollView(this.View.Bounds);
scrollView.MaximumZoomScale = 4f;
scrollView.MinimumZoomScale = 1f;
scrollView.ViewForZoomingInScrollView += (UIScrollView sv) =>
{
    scrollView.ContentSize = new SizeF(imageView.Frame.Width, imageView.Frame.Height);
    return imageView;
};

this.View.AddSubview(scrollView);

scrollView.AddSubview(imageView);

我可以很好地缩放图像,UIImageView的宽度和高度也可以很好地修改。 scrollview的contentSize也会更新,但是当缩放时,我只能平移到图像的某个宽度,而不是整个宽度。我也无法平息。我可以平移到最左上角。

有谁知道这可能是什么?

图片:http://i.stack.imgur.com/VPkgO.jpg

图像缩放:http://i.stack.imgur.com/AQl61.jpg

平移图像:http://i.stack.imgur.com/fR1Z3.jpg

1 个答案:

答案 0 :(得分:0)

哦,我已经找到了解决方案。显然,用于父scrollView的View.Bounds是错误的。滚动视图几乎是实际屏幕尺寸的2倍,因此平移不起作用(因为滚动视图的宽度比实际屏幕尺寸宽)。

通过修改scrollView的初始化来解决它:

Set Nocount On;

Declare @students Table
(
     Id                 Int Identity(1,1)
    ,Name               Varchar(100)
    ,CurrentRank        Int
)

Insert Into @students(Name) Values
 ('Alex')
,('Peter')
,('Mark')
,('Jhon')
,('Steven')

Declare @results Table
(
     RowId              Int Identity(1,1)
    ,Id                 Int
    ,Marks              Int
    ,ResultYear         Int
)

Insert Into @results(Id,Marks,ResultYear) Values
 (1,32,2015)
,(1,29,2015)
,(1,31,2015)
,(2,40,2015)
,(2,39,2015)
,(2,38,2015)
,(3,18,2015)
,(3,25,2015)
,(3,29,2015)
,(4,35,2015)
,(4,31,2015)
,(4,21,2015)
,(5,25,2015)
,(5,33,2015)
,(5,40,2015)

---- 1. update with join
Update  s
Set     s.CurrentRank = r.ResultRank
From    @students As s
        Join
        (
            Select   Id
                    ,Row_Number() Over(Order By Avg(r.Marks) Desc) As ResultRank
            From    @results As r
            Where   r.ResultYear = 2015
            Group By r.Id
        ) As r On s.Id = r.Id

---- 2. update by where
Update  @students
Set     CurrentRank = r.ResultRank
From    (
            Select   Id As StudentId
                    ,Row_Number() Over(Order By Avg(r.Marks) Desc) As ResultRank
            From    @results As r
            Where   r.ResultYear = 2015
            Group By r.Id
        ) As r
Where   Id = r.StudentId