Slider view for iOS

2019-05-11 19:18发布

----window1----- ----window2----- Can someone help me with this scenario?

*There is a button, which when tapped, slides opens up a UIView, with the tapped button still to its left.

*This button when tapped again, makes the UIView slide back.

3条回答
Deceive 欺骗
2楼-- · 2019-05-11 19:23

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) {

                         }
                     }];
}
查看更多
做自己的国王
3楼-- · 2019-05-11 19:30

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.

查看更多
Evening l夕情丶
4楼-- · 2019-05-11 19:49

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;
}
查看更多
登录 后发表回答