gpt4 book ai didi

ios - 如何基于平移手势放大/缩小 UIView?

转载 作者:行者123 更新时间:2023-12-03 19:17:34 33 4
gpt4 key购买 nike

我想基于UIPanGesture放大/缩小UIView。我不知道如何将 translationInView 转换为 Scaling 参数。 (我知道如何根据捏合手势放大/缩小)。

我正在尝试弄清楚如何做到这一点,但到目前为止还没有这样的运气。

最佳答案

要进行缩放,您需要 UIPinchGestureRecognizer,而不是平移

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {    
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}

另请检查this链接。

更新:

从 UIPanGestureRecognizer 接收触摸信息的唯一方法是

- (NSUInteger)numberOfTouches;
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view;

所以也许你可以尝试这样的事情:

    CGFloat old_distance = 0.0; // keep it somewhere between touches!

- (void)panGestureRecognized:(UIPanGestureRecognizer *)recognizer {
if (recognizer.numberOfTouches == 2) {
CGPoint a = [recognizer locationOfTouch:0 inView:recognizer.view];
CGPoint b = [recognizer locationOfTouch:1 inView:recognizer.view];
CGFloat xDist = (a.x - b.x);
CGFloat yDist = (a.y - b.y);
CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));

CGFloat scale = 1;
if (old_distance != 0) {
scale = distance / old_distance;
}
old_distance = distance;
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, scale, scale);
}
}

关于ios - 如何基于平移手势放大/缩小 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22349571/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com