【问题标题】:Having ScrollView.contentSize issues not able to scroll properly on Top, Left, Right?ScrollView.contentSize 问题无法在顶部、左侧、右侧正确滚动?
【发布时间】:2013-12-18 06:59:35
【问题描述】:

在 Scroll-View 中有一个 Child-View 并在 Scroll on Top、Left、Right 中遇到问题?

在子子视图中添加捏合和缩放的代码

 UIPinchGestureRecognizer *twoFingerPinch = [[[UIPinchGestureRecognizer alloc]
                                                 initWithTarget:self
                                                 action:@selector(twoFingerPinch:)]
                                                autorelease];

    [_childView addGestureRecognizer:twoFingerPinch];

捏和缩放子视图的方法

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
    //NSLog(@"Pinch scale: %f", recognizer.scale);
    CGAffineTransform transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);
    // you can implement any int/float value in context of what scale you want to zoom in or out
    _childView.transform = transform;

    _scrollView.contentSize =
    CGSizeMake(_scrollView.frame.size.width,_childView.frame.size.height);

}

Scroll-View ContentSize 设置有什么问题吗?

这是我的代码

Code Link

谢谢

【问题讨论】:

标签: ios objective-c uiscrollview contentsize


【解决方案1】:

编辑:

设置_scrollview framesize和_childview framesize相同:

//scrollview 56、92、196、408的帧。

_scrollView.delegate = self;

_scrollView.minimumZoomScale = 1.0;
_scrollView.maximumZoomScale = 10;

_childView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 196, 408)];

_childView.backgroundColor=[UIColor blackColor];

[_scrollView addSubview:_childView];

 [_scrollView setContentSize:_childView.frame.size];

并实现这个委托

 - (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
 {
    return _childView;
  }

还要检查您在 XIB 中的自动调整大小标志。它设置为下边距。

【讨论】:

  • 不工作 :(。左侧和顶部滚动不起作用我无法看到 childView 的左上角。
  • 检查您的滚动视图及其超级视图框架。可以的话可以上传截图吗?
【解决方案2】:

您正在将 scrollView 的宽度传递给 contentsize。您应该传递 childView 的宽度。

_scrollView.contentSize = CGSizeMake(_chilView.frame.size.width,_childView.frame.size.height);

【讨论】:

  • 不工作 :(。左侧和顶部滚动不起作用我无法看到 childView 的左上角。
【解决方案3】:

你不应该在CGAffineTransform之后得到_childView.frame.size.height。

This question解释清楚。

【讨论】:

    【解决方案4】:

    我不能 100% 确定您当前的情况。

    但是在iOS5之后,UIScrollView 有以下内容:-

    // `pinchGestureRecognizer` will return nil when zooming is disabled.
    @property(nonatomic, readonly) UIPinchGestureRecognizer *pinchGestureRecognizer NS_AVAILABLE_IOS(5_0);
    

    从描述看来,如果你设置了这些-

    @property(nonatomic) float minimumZoomScale;     // default is 1.0
    @property(nonatomic) float maximumZoomScale;     // default is 1.0. must be > minimum zoom scale to enable zooming
    

    您可以使用这些属性简单地启用缩放。放大和缩小应该会自动为您工作,而无需实现您自己的 UIGestureRecognizer。

    【讨论】:

      【解决方案5】:

      我已经删除了所有代码并使用以下代码进行了更改我犯了非常愚蠢的错误:)

         _scrollView.contentSize = CGSizeMake(_childView.frame.size.width, _childView.frame.size.height);
              _scrollView.maximumZoomScale = 6.0;
              _scrollView.minimumZoomScale = 1.0;
              _scrollView.clipsToBounds = YES;
              _scrollView.delegate = self;
      
      
          - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
          //    NSLog(@"View - %@\n",_childView);
              return _childView;
          }
      

      感谢@Mavericks

      :)

      【讨论】:

        猜你喜欢
        • 2021-06-14
        • 1970-01-01
        • 2013-07-29
        • 2011-06-22
        • 2019-08-25
        • 1970-01-01
        • 2016-07-10
        • 2014-10-15
        • 1970-01-01
        相关资源
        最近更新 更多