Slider view for iOS

我们两清 提交于 2019-12-06 03:26:40

What you describe is easy. Let's call the view that slides in from the right a drawer ("drawerView"). Set up the drawer view as a child view of your view controller's main view.

Make that "drawer" view a container view. Put everything you want inside it. (Your text view, buttons, etc.) Also put your button inside this view. Connect that button to an action "slideDrawer" in your view controller.

Then make sure "clips subviews" is false, and move the button off the left edge of the drawer view with the left arrow key. In IB it will disappear, but don't worry. IB doesn't honor the "clips subviews" flag like your running program will.

Create an outlet to your drawerView and link it up to your code.

Once you have your drawer view looking exactly how you want it, note it's x coordinate in the "size inspector". Let's call that value kVisibleX. Then drag that view to the right until it's just off-screen. The button won't be visible in IB, but will be visible just at the edge of the window at run-time. (like you show in your first image)

Note the x coordinate of the drawer view when it's offscreen. Let's call that value kOffscreenX.

Add a boolean instance variable "drawerIsShowing" to your view controller.

Now write an IBAction method slideDrawer:

- (IBAction) slideDrawer: (id) sender;

In that method, check drawerIsShowing to see if the drawer is currently visible. If it is, slide it off-screen. If it's not, slide it on-screen.

- (IBAction) slideDrawer: (id) sender;
{
  CGFloat newX;
  if (drawerIsShowing)
    newX = kOffscreenX;
  else
    newX = kVisibleX;
  [UIView animateWithDuration: .25
  animations: 
  ^{
    CGRect drawerFrame = drawerView.frame;
    drawerFrame.origin.x = newX;
    drawerView.frame = drawerFrame;
  }
drawerIsShowing = !drawerIsShowing;
}

I would have window 1 be view1 and window 2 be view2. Populate each of these views with whatever needs to be in the view. If you want the same button to be in both views, place a button into each view that has the same text. To the user it is the same button. Have the action of the button on view 1 push to view 2. View 2 will initialize with whatever you have told it to. Pushing the button in view 2 will pop the view and return it to view 1...

I hope that helps. If you need more specific help, provide more information and I should be able to help you further.

Declare a BOOL to check view is hidden or not.

In .h

BOOL _isContentVisible;

In .m

-(IBAction)showHideContentView:(id)sender
{
    //BOOL isContentVisible= CGRectIntersectsRect(self.view.bounds, _sideContentScrollView.frame);

    [UIView animateWithDuration:1.0f
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         //hide if visible else show
                         if (_isContentVisible) { // Hide
                             _isContentVisible = NO;
                             [_sideContentView setFrame:CGRectMake(-320, 0, 360, 748)];// 40 is assumed to button size
                         }
                         else { // Show
                             _isContentVisible = YES;
                             [_sideContentView setFrame:CGRectMake(0, 0, 360, 748)];
                         }
                     }
                     completion:^(BOOL finished) {
                         if (finished) {

                         }
                     }];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!