在您的平移手势处理程序中,您只需抓住translationInView
or locationInView
,从中计算比例,然后相应地更新变换。例如:
- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
static CGAffineTransform originalTransform;
if (gesture.state == UIGestureRecognizerStateBegan)
{
originalTransform = self.viewToScale.transform;
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [gesture translationInView:gesture.view];
CGFloat scale = 1.0 - translation.y / 160.0;
self.viewToScale.transform = CGAffineTransformScale(originalTransform, scale, scale);
}
}
您可以scale
根据您想要做什么来进行计算,但希望您能明白这一点。
就个人而言,我宁愿使用捏合手势识别器来调整大小(它是用户接受过培训的 UI,它为您提供了scale
开箱即用的因素等),但无论什么都适合您。如果你做了一个捏手势识别器,它可能看起来像:
- (void)handlePinch:(UIPinchGestureRecognizer *)gesture
{
static CGAffineTransform originalTransform;
if (gesture.state == UIGestureRecognizerStateBegan)
{
originalTransform = self.viewToScale.transform;
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
self.viewToScale.transform = CGAffineTransformScale(originalTransform, gesture.scale, gesture.scale);
}
}