如何约束UIScrollView仅垂直缩放? [英] How to constrain UIScrollView to only zoom vertically?

查看:111
本文介绍了如何约束UIScrollView仅垂直缩放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIScrollView,用于在图形上表示轴。我希望用户能够使用常规的捏合运动来放大轴,但要使其仅在垂直方向缩放,而不是水平缩放。



我的问题类似于,但是我尝试了那里建议的解决方案(重写子视图的SetTransform方法,以便它忽略一个方向的缩放),并且在水平(而非垂直)约束缩放时,它可以完美地工作。当我尝试垂直实现该功能时,第一个捏合操作效果很好,但是随后的捏合似乎将缩放比例重置为一个缩放比例,然后才生效。导致这种行为,更重要的是如何解决?



我正在使用MonoTouch,但使用Objective-C的答案就可以了。

解决方案

我知道这个问题已经发布了很久了,但是对于那些坚持此问题的人来说,这是一个答案。



我查看了您链接到的问题rankAmateur,我认为解决在那里找到的解决方案以适应您的需求的简单方法是替换CGAffineTransform的 a

 -(void)setTransform:(CGAffineTransform)newValue; 
{
CGAffineTransform constrainedTransform = CGAffineTransformIdentity;
// constrainedTransform.a = newValue.a;
constrainedTransform.d = newValue.d;
[super setTransform:constrainedTransform];
}

我对CGAffineTransorm不太了解,但这对我和浏览文档后,似乎 a属性对应于视图的x轴,而 d属性对应于视图的y轴。



编辑



因此,回过头来意识到问题的实质之后,我对此进行了更多的研究,但我感到有些困惑,但是经历了与RankAmateur上面提到的行为相同,当缩放限制为仅水平放置,但限制为仅垂直放置时,CGAffineTransform与zoomScale完美配合使用似乎非常不寻常。



我唯一可以提供的假设是,它可能与Core Graphics和UIKit的不同默认坐标系有关,因为在这些坐标系中,x-轴的功能相同,而y轴的功能相反。也许以某种方式使它在前面提到的setTransform的重写中变得混乱了。


I have a UIScrollView which I'm using to represent an axis on a graph. I'd like the user to be able to zoom in on the axis using the usual pinch motion, but for it to only scale in the vertical direction, not horizontally.

My question is similar to this one, but I've tried the solution suggested there (overriding the subview's SetTransform method so that it ignores scaling in one direction) and it works perfectly when constraining scaling horizontally, but not vertically. When I try implementing it vertically the first pinch action works fine, but subsequent pinches seem to reset the zoom scale to one before having any effect.

Does anyone know what might be causing this behaviour, and more importantly how I can get around it?

I'm using MonoTouch but answers using Objective-C are fine.

解决方案

I know this question was posted quite a while ago, but here's an answer for anyone stuck on this problem.

I looked over the question you linked to, rankAmateur, and I think the simple way to fix the solution found there to suit your needs is to replace the CGAffineTransform's "a" property with its "d" property in the setTransform: method.

- (void)setTransform:(CGAffineTransform)newValue;
{
 CGAffineTransform constrainedTransform = CGAffineTransformIdentity;
 // constrainedTransform.a = newValue.a;
 constrainedTransform.d = newValue.d;
 [super setTransform:constrainedTransform];
}

I'm not very well versed in CGAffineTransorm, but this worked for me and after browsing the documentation it seems the "a" property corresponds to a view's x-axis and the "d" property corresponds to a view's y-axis.

EDIT

So after going back and realizing what the question really was, I did some more digging into this and I'm a bit stumped, but having experienced the same behavior that rankAmateur mentions above, it seems incredibly unusual for the CGAffineTransform to work perfectly well with zoomScale when zooming is constrained to only horizontally, but not when constrained to only vertically.

The only hypothesis I can offer, is that it might have something to do with the differing default coordinate systems of Core Graphics and UIKit, since in those coordinate systems the x-axis functions in the same way, while the y-axis functions oppositely. Perhaps somehow this gets muddled up in the previously mentioned overriding of setTransform.

这篇关于如何约束UIScrollView仅垂直缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆