2

我正在开发文档查看器。文档显示在 aUIScrollView中,以便可以滚动和缩放。我需要在文档周围画一个边框,以便在视觉上将它与UIScrollView. 边框不得与文档一起缩放 - 无论缩放比例如何,它都应保持恒定的厚度。

我当前的设置包括一个UIScrollView有两个UIView孩子的——一个用于文档,一个用于边框。我已经覆盖viewForZoomingInScrollView: 以返回文档视图。我还重写layoutSubviews以使文档视图居中(以防它小于UIScrollView),然后调整边框视图的大小并将其定位在其后面,使其看起来像一个框架。当用户手动滚动和缩放时,这可以正常工作。但是当我使用zoomToRect:animated:以编程方式缩放时,layoutSubviews 在动画开始之前被调用,并且我的边框视图会立即调整大小,而文档视图稍后会赶上。

澄清:边框需要紧密贴合文档视图而不是UIScrollView自身。

4

4 回答 4

3

示例代码:

yourScrollView.layer.cornerRadius=8.0f;
yourScrollView.layer.masksToBounds=YES;
yourScrollView.layer.borderColor=[[UIColor redColor]CGColor];
yourScrollView.layer.borderWidth= 1.0f;

不要忘记: #Import <QuartzCore/QuartzCore.h>

于 2013-05-27T12:36:36.653 回答
1

最后,我能够通过动画缩放解决问题。我的设置与问题中描述的相同。我只是在我的layoutSubview实现中添加了一些代码,以便检测任何正在运行的UIScrollView动画并将其与类似的动画相匹配以调整边框的大小。

这是代码:

- (void)layoutSubviews
{
  [super layoutSubviews];

  // _pageView displays the document
  // _borderView represents the border around it

  . . .


  // _pageView is now centered -- we have to move/resize _borderView
  // layers backing a view are not implicitly animated
  // the following two lines work just fine if we don't need animation
  _borderView.layer.bounds = CGRectMake(0.0, 0.0, frameToCenter.size.width + 2.0 * 15.0, frameToCenter.size.height + 2.0 * 15.0);
  _borderView.layer.position = _pageView.center;

  if (_pageView.layer.animationKeys.count > 0)
  {
    // UIScrollView is animating its content (_pageView)
    // so we need to setup a matching animation for _borderView

    [CATransaction begin];

    CAAnimation *animation = [_pageView.layer animationForKey:[_pageView.layer.animationKeys lastObject]];
    CFTimeInterval beginTime = animation.beginTime;
    CFTimeInterval duration = animation.duration;

    if (beginTime != 0.0) // 0.0 means the animation starts now
    {
      CFTimeInterval currentTime = [_pageView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
      duration = MAX(beginTime + duration - currentTime, 0.0);
    }

    [CATransaction setAnimationDuration:duration];
    [CATransaction setAnimationTimingFunction:animation.timingFunction];

    // calculate the initial state for _borderView animation from _pageView presentation layer
    CGPoint presentationPos = [_pageView.layer.presentationLayer position];
    CGRect presentationBounds = [_pageView.layer.presentationLayer frame];
    presentationBounds.origin = CGPointZero;
    presentationBounds.size.width += 2.0 * 15.0;
    presentationBounds.size.height += 2.0 * 15.0;

    CABasicAnimation *boundsAnim = [CABasicAnimation animationWithKeyPath:@"bounds"];
    boundsAnim.fromValue = [NSValue valueWithCGRect:presentationBounds];
    boundsAnim.toValue = [NSValue valueWithCGRect:_borderView.layer.bounds];
    [_borderView.layer addAnimation:boundsAnim forKey:@"bounds"];

    CABasicAnimation *posAnim = [CABasicAnimation animationWithKeyPath:@"position"];
    posAnim.fromValue = [NSValue valueWithCGPoint:presentationPos];
    posAnim.toValue = [NSValue valueWithCGPoint:_borderView.layer.position];
    [_borderView.layer addAnimation:posAnim forKey:@"position"];

    [CATransaction commit];
  }

}

它看起来很hacky,但它有效。我希望我不必UIScrollView为了让简单的边框在动画期间看起来不错而进行逆向工程......

于 2013-06-10T08:43:08.540 回答
1

首先,您需要将 QuartzCore 框架导入您的应用程序。然后在要设置边框的类上导入该 .h 文件。像这样。

#import <QuartzCore/QuartzCore.h>

边框设置。

ScrollView.layer.cornerRadius=5.0f;
ScrollView.layer.masksToBounds=YES;
ScrollView.layer.borderColor=[[UIColor redColor]CGColor];
ScrollView.layer.borderWidth= 4.0f;

检查这个对你真的很有帮助。

于 2013-05-27T12:55:18.670 回答
0

导入 QuartzCore 框架:

#import <QuartzCore/QuartzCore.h>

现在为它的视图添加颜色:

[scrollViewObj.layer setBorderColor:[[UIColor redColor] CGColor]];

[scrollViewObj.layer setBorderWidth:2.0f];
于 2013-05-27T12:38:24.233 回答