修改 SWRevealViewController 中 frontView 的 frame

Modify frame of the frontView in SWRevealViewController

通常情况下,如果使用 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 中已处理平移和点击手势的位置调​​用。

这是滑动时视图的样子:

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

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

    }

}

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