菜鸟教程小白 发表于 2022-12-13 12:39:00

ios - 通过 UIPinchGestureRecognizer 设置相对于缩放变换的 ScrollView 的内容大小


                                            <p><p>我有 ScrollView 并将 <code>UIPinchGestureRecognizer</code> 添加到 ScrollView 。现在在手势事件中,我放大或缩小 ScrollView ,其句柄操作如下:</p>

<pre><code>static float lastScale = 1.0;

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer
{

    if( == UIGestureRecognizerStateBegan) {
    // Reset the last scale, necessary if there are multiple objects with different scales
      lastScale = ;
    }

    if ( == UIGestureRecognizerStateBegan || == UIGestureRecognizerStateChanged)
    {
      CGFloat currentScale = [[.layer valueForKeyPath:@&#34;transform.scale&#34;] floatValue];

      // Constants to adjust the max/min values of zoom
      const CGFloat kMaxScale = 2.0;
      const CGFloat kMinScale = 1.0;

      CGFloat newScale = 1 -(lastScale - );
      newScale = MIN(newScale, kMaxScale / currentScale);
      newScale = MAX(newScale, kMinScale / currentScale);
      CGAffineTransform transform = CGAffineTransformScale([ transform], newScale, newScale);
      .transform = transform;

      lastScale = ;// Store the previous scale factor for the next pinch gesture call
    }
}
</code></pre>

<p>我想在这个方法中改变 contentSize。现在,我的问题是如何设置 ScrollView 的内容大小,以便在缩小后我可以滚动查看 ScrollView 中的所有内容。</p>

<p>请帮忙。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><pre><code>@interface HorizontalScrollView ()
{
    UIView *container;
}
@end

@implementation HorizontalScrollView
@synthesize scrlView;

- (void)viewDidLoad {
    ;
    // Do any additional setup after loading the view.

    container = [initWithFrame:CGRectMake(0, 0, self.scrlView.frame.size.width, self.scrlView.frame.size.height)];


    UIImage *image = ;
    UIImageView *imageView = [ initWithImage:image];
    CGRect rect = imageView.frame;
    rect.size.height = 215;
    rect.size.width = self.scrlView.frame.size.width;
    rect.origin = CGPointMake(0, 0);
    imageView.frame = rect;
    ;

    ;

    self.scrlView.minimumZoomScale=1.0;
    self.scrlView.maximumZoomScale=6.0;
    self.scrlView.delegate=self;

    // set the content size so it can be scrollable
    ;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return container;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 通过 UIPinchGestureRecognizer 设置相对于缩放变换的 ScrollView 的内容大小,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/34281827/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/34281827/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 通过 UIPinchGestureRecognizer 设置相对于缩放变换的 ScrollView 的内容大小