2013-05-27 55 views
2

我正在处理文档查看器。文档显示在UIScrollView内部,以便它可以滚动和缩放。我需要在文档周围画一个边框,以便从UIScrollView的背景中可视化地分离它。不能将边框与文档放在一起 - 不管缩放比例如何,它都应保持不变的厚度。如何围绕UIScrollView的内容绘制边框?

我目前的设置由一个UIScrollView和两个UIView孩子组成 - 一个用于文档,另一个用于边框。我重写了viewForZoomingInScrollView:返回文档视图。我也重写了layoutSubviews以居中文档视图(如果它小于UIScrollView),然后调整其大小并将边框视图放置在其后面,以使其看起来像一个框架。当用户手动滚动和缩放时,这可以正常工作。但是,当我使用zoomToRect:animated:进行编程缩放时,在动画开始之前调用layoutSubviews,并在文档视图稍后赶上时立即调整边框视图的大小。

说明:边框需要紧贴文档视图,而不是围绕UIScrollView本身。

回答

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

} 

它看起来哈克,但它的工作原理。我希望我没有反向工程UIScrollView为了在动画过程中使一个简单的边框看起来不错...

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>

+0

边框必须围绕内容视图而不是围绕滚动视图本身 – itotsev

+0

@itotsev:没有问题在所有。你可以在'yourScrollView'处使用'yourContentView'。我只是简单地给你一个总想法。 – Bhavin

+0

如果我这样做,当用户放大'UIScrollView'时,边框会变得更厚,我希望它与缩放比例无关。 – itotsev

0

进口QuartzCore框架:

#import <QuartzCore/QuartzCore.h> 

现在添加颜色的观点:

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

[scrollViewObj.layer setBorderWidth:2.0f]; 
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; 

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