UIView 特定部分缩放 in/out 不增加 hight/width View objective C iOS

Specfic part of UIView zoom in/out without increasing hight/width of View objective C iOS

我实现了这个。但问题是它增加了视图的 hight/width 和 zooming.How 来缩放而不增加 hight/width。我的 UIView hight/width 是 fixed.My 基本目标是视图 hight/width 保持不变但缩放视图的内侧。

    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
    }
}

在视图上使用变换时,其框架的 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 表示抱歉,但这对您的情况应该没有影响)

当为这 2 个视图打印框架时,结果是

baseView: (-50 -50; 150 150)
addedView: (0 0; 50 50)

但从视觉上看,这 2 个视图是相同的,添加的视图与基础视图完全重叠。

在您的情况下,没有足够的代码来显示特定的修复,但如果您为当前拥有的视图创建一个基础视图并缩放基础视图,那么您的框架更改问题应该得到解决。

在 yourScroll 中添加您的视图作为子视图。

[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;
}