我实现了这个。但问题是它通过缩放增加了视图的高度/宽度。如何在不增加高度/宽度的情况下进行缩放。我的UIView高度/宽度是固定的。我的基本目标是视图高度/宽度保持相同但缩放视图的内侧。
UIPinchGestureRecognizer * pinGeture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(twoFingerPinch:)];
[radarViewHolder addGestureRecognizer:pinGeture];
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
NSLog(@"Pinch scale: %f", recognizer.scale);
if([recognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [recognizer scale];
}
if ([recognizer state] == UIGestureRecognizerStateBegan ||
[recognizer state] == UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[[recognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];
// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;
CGFloat newScale = 1 - (lastScale - [recognizer scale]);
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[recognizer view] transform], newScale, newScale);
[recognizer view].transform = transform;
lastScale = [recognizer scale]; // Store the previous scale factor for the next pinch gesture call
}
}
@ShahbazAkram声明一个浮点变量CGFloat lastScale;
将此方法替换为您的代码。
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
NSLog(@"Pinch scale: %f", recognizer.scale);
if([recognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [recognizer scale];
}
if ([recognizer state] == UIGestureRecognizerStateBegan ||
[recognizer state] == UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[[recognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];
// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;
CGFloat newScale = 1 - (lastScale - [recognizer scale]);
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[recognizer view] transform], newScale, newScale);
[recognizer view].transform = transform;
lastScale = [recognizer scale]; // Store the previous scale factor for the next pinch gesture call
}
}
将您的视图添加到yourScroll中作为subView。
[yourScroll addSubView:radarViewHolder];
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
CGFloat size = scrollView.zoomScale;
if (size > ZoomMaxValue)
{
yourScroll.zoomScale = maximumScale;
}
if (size > ZoomMinValue && size < ZoomValue && size < 1.01)
{
yourScroll.zoomScale = minimumScale;
}
ZoomValue = size;
}
在视图上使用变换时,其帧的getter将使其坐标与此变换相乘。因此内部框架没有真正更新。
这通常不是问题,但在某些情况下可能是。通常,要避免此类情况,您只需将视图添加到另一个视图,然后缩放超级视图。假设这样的代码:
let baseView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 50.0, height: 50.0))
baseView.backgroundColor = .red
let addedView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 50.0, height: 50.0))
addedView.backgroundColor = .green
baseView.addSubview(addedView)
baseView.transform = CGAffineTransform(scaleX: 3.0, y: 3.0)
(抱歉Swift但你的情况应该没有区别)
当打印这两个视图的帧时,结果是
baseView: (-50 -50; 150 150)
addedView: (0 0; 50 50)
但在视觉上这两个视图是相同的,并且添加的视图完全重叠基本视图。
在您的情况下,没有足够的代码来显示特定的修复,但如果您为当前拥有的视图创建基本视图并缩放基本视图,则应修复更改框架的问题。