ios - 从当前比例捏/缩放
Posted
技术标签:
【中文标题】ios - 从当前比例捏/缩放【英文标题】:ios - Pinch/zoom from current scale 【发布时间】:2012-05-28 06:02:22 【问题描述】:以下代码正确地捏合/缩放容器视图,但仅在它跳转到 1.0 的比例之后。如何修改它以使容器视图从当前比例缩放?
UIPinchGestureRecognizer *twoFingerPinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)];
[self.container addGestureRecognizer:twoFingerPinch];
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
_scale = recognizer.scale;
CGAffineTransform tr = CGAffineTransformScale(self.view.transform, _scale, _scale);
self.container.transform = tr;
【问题讨论】:
您的问题解决了吗?如果是,请分享代码。 【参考方案1】:在.h文件中,添加:
CGFloat _lastScale;
在.m文件中,
- (id)init
...
_lastScale = 1.0f;
...
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
if (recognizer.state == UIGestureRecognizerStateEnded)
_lastScale = 1.0f;
return;
CGFloat scale = 1.0f - (_lastScale - recognizer.scale);
CGAffineTransform tr = CGAffineTransformScale(self.view.transform, scale, scale);
self.container.transform = tr;
_lastScale = recognizer.scale;
【讨论】:
_lastScale 和识别器.scale 总是几乎相同,所以比例总是非常接近 1.0,所以视图几乎不会放大或缩小。【参考方案2】:我是这样做的:
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer
static float initialDifference = 0.0;
static float oldScale = 1.0;
if (recognizer.state == UIGestureRecognizerStateBegan)
initialDifference = oldScale - recognizer.scale;
CGFloat scale = oldScale - (oldScale - recognizer.scale) + initialDifference;
myView.transform = CGAffineTransformScale(self.view.transform, scale, scale);
oldScale = scale;
【讨论】:
这需要更多编码才能缩小。以上是关于ios - 从当前比例捏/缩放的主要内容,如果未能解决你的问题,请参考以下文章