【问题标题】:Modify frame of the frontView in SWRevealViewController在 SWRevealViewController 中修改 frontView 的框架
【发布时间】:2015-04-19 03:32:04
【问题描述】:

通常,如果使用 SWRevealViewController,前视图会完全覆盖后视图,而后视图只有在执行某些操作(滑动、点击等)时才可见。我的要求是,当视图加载和用户向后滑动时,后视图至少应该始终可见。我尝试修改frontView的框架来实现这一点,但我没有看到任何效果。

我把帧更新帧代码放在一个方法里:

-(void)updateFrontViewControllerFrameWithPadding:(BOOL)bProvidePadding
{
    if (!bProvidePadding) {

        CGRect frontViewControllerFrame = self.frontViewController.view.bounds;

        CGFloat xOrigin = self.view.bounds.origin.x;
        CGFloat nWidth = self.view.bounds.size.width;

        frontViewControllerFrame.origin.x = xOrigin;
        frontViewControllerFrame.size.width = nWidth;

        self.frontViewController.view.frame = frontViewControllerFrame;

        _frontViewShadowColor = [UIColor blackColor];

        [_contentView reloadShadow];
    }

    else {

        CGFloat nPadding = 0.0f;

        if ([IILUtility targetDevice] == UIUserInterfaceIdiomPad) {

            _rearViewRevealWidthIpad = 20.0f;

            nPadding = _rearViewRevealWidthIpad;
        }
        else {

            _rearViewRevealWidthIphone = 15.0f;

            nPadding = _rearViewRevealWidthIphone;
        }

        CGRect revealViewBounds = self.view.bounds;

        CGFloat frontViewXPos = revealViewBounds.origin.x + nPadding;
        CGFloat frontViewWidth = revealViewBounds.size.width - nPadding;

        CGRect frontViewFrame = self.frontViewController.view.frame;

        frontViewFrame.origin.x = frontViewXPos;
        frontViewFrame.size.width = frontViewWidth;

        self.frontViewController.view.frame = frontViewFrame;

        _frontViewShadowColor = [UIColor colorWithWhite:0.5
                                                  alpha:0.5];

        [_contentView reloadShadow];

        //reset rearViewRevealWidth
        [self resetRearViewRevealWidth];

    }
}

此方法从 viewWillLayoutSubviews 方法调用,providePadding 参数为 TRUE。此方法还需要从 SWRevealViewController 中已处理平移和点击手势的位置调​​用。

这是滑动时视图的外观:

【问题讨论】:

  • 由于缩小了前视图的框架,边框占用了所有空间。如何防止这种情况发生?当我慢慢滑动前视图时,我注意到后视图被加载,好像它的宽度随着滑动动作的执行而增加。是这样吗?如果是这样,后视图的初始宽度是否等于零?如何将其设置为不同的值?

标签: ios objective-c swrevealviewcontroller


【解决方案1】:

这是调整框架大小问题的解决方案:

-(void)updateFrontViewControllerFrameWithPadding:(BOOL)bProvidePadding
{

    if (!bProvidePadding) {

        CGRect frontViewControllerFrame = self.frontViewController.view.bounds;

        CGFloat xOrigin = self.view.bounds.origin.x;
        CGFloat nWidth = self.view.bounds.size.width;

        frontViewControllerFrame.origin.x = xOrigin;
        frontViewControllerFrame.size.width = nWidth;

        self.frontViewController.view.frame = frontViewControllerFrame;

        _frontViewShadowColor = [UIColor blackColor];

        [_contentView reloadShadow];


        //There is a catch here. The shadow for _frontView is different from that of self.frontViewController.view
        //If we do not modify the shadow for self.frontViewController.view then it overlaps with
        // the shadow of _frontView. This is generally not a problem in the case when the rear view is in focus and the
        //front view is toggled to the right. But if we need the front view to be shifted slighlty to the right even when
        //its position is FrontViewPositionLeft, it is important to manage the shadow of self.frontViewController.view
        //otherwise the shadow takes up width equal to the padding we have provided.
        CALayer *frontViewLayer = self.frontViewController.view.layer;
        frontViewLayer.shadowColor = [UIColor colorWithWhite:0.8f
                                                       alpha:0.0f].CGColor;
        frontViewLayer.shadowOpacity = 0.0f;
        frontViewLayer.shadowOffset = CGSizeMake(0.0f, 0.0f);
        frontViewLayer.shadowRadius = 0.0f;

    }

    else {

        CGFloat nPadding = 0.0f;

        if ([IILUtility targetDevice] == UIUserInterfaceIdiomPad) {

            _rearViewRevealWidthIpad = 60.0f;

            nPadding = _rearViewRevealWidthIpad;
        }
        else {

            _rearViewRevealWidthIphone = 35.0f;

            nPadding = _rearViewRevealWidthIphone;
        }

        CGRect revealViewBounds = self.view.bounds;

        CGFloat frontViewXPos = revealViewBounds.origin.x + nPadding;
        CGFloat frontViewWidth = revealViewBounds.size.width - nPadding;

        CGRect frontViewFrame = self.frontViewController.view.frame;

        frontViewFrame.origin.x = frontViewXPos;
        frontViewFrame.size.width = frontViewWidth;

        self.frontViewController.view.frame = frontViewFrame;

        [self rearViewDeploymentForLeftPosition];

        _frontViewShadowColor = [UIColor colorWithWhite:0.8f
                                                  alpha:0.0f];

        [_contentView reloadShadow];

        //There is a catch here. The shadow for _frontView is different from that of self.frontViewController.view
        //If we do not modify the shadow for self.frontViewController.view then it overlaps with
        // the shadow of _frontView. This is generally not a problem in the case when the rear view is in focus and the
        //front view is toggled to the right. But if we need the front view to be shifted slighlty to the right even when
        //its position is FrontViewPositionLeft, it is important to manage the shadow of self.frontViewController.view
        //otherwise the shadow takes up width equal to the padding we have provided.
        CALayer *frontViewLayer = self.frontViewController.view.layer;
        frontViewLayer.shadowColor = [UIColor blackColor].CGColor;
        frontViewLayer.shadowOpacity = _frontViewShadowOpacity;
        frontViewLayer.shadowOffset = _frontViewShadowOffset;
        frontViewLayer.shadowRadius = _frontViewShadowRadius;

        //reset rearViewRevealWidth
        [self resetRearViewRevealWidth];

    }

}

这也考虑到了上面提到的阴影相关问题。 这个方法需要从适当的地方调用,例如;最初加载视图时,执行平移或点击手势等时。

【讨论】:

    猜你喜欢
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多