开发者

Pull Menu in CocoaTouch

开发者 https://www.devze.com 2023-01-18 16:22 出处:网络
I am attempting to make a UIView/UIControl that people can drag up and reveal a text box, and then drag down to hide it.However, I have yet to find a method to make this \"fluid\" - it always seems to

I am attempting to make a UIView/UIControl that people can drag up and reveal a text box, and then drag down to hide it. However, I have yet to find a method to make this "fluid" - it always seems to stop at random places and doesn't allow any more movement. At the moment I am using a UIView for the top part of the view and here is the current code:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

 UITouch *touch = [touches anyObject];

 if ([touch view] == topMenuView) {
  CGPoint location = [touch locat开发者_JAVA技巧ionInView:self.superview];

  CGPoint locationInsideBox = [touch locationInView:self];

  CGPoint newLocation = location;
  newLocation.x = self.center.x;
  newLocation.y = newLocation.y + (self.frame.size.height - locationInsideBox.y) / 2;

  if ((self.superview.frame.size.height - newLocation.y) < (self.frame.size.height / 2) && (self.superview.frame.size.height - newLocation.y) > -32)
  {
   self.center = newLocation;
  }
  return;
 }
}

Any help would be much appreciated!


I would use a pan gesture recognizer. The following code will simply move the view up and down along with the user's finger. If you want to limit how far up it moves, have it snap to place or have momentum you'll need to add to it.

UIView * view; // The view you're moving
CGRect originalFrame; // The frame of the view when the touch began

- (void) pan:(UIPanGestureRecognizer *)pan {
    switch (pan.state) {
        case UIGestureRecognizerStateBegan: {
            originalFrame = view.frame;
        } break;

        case UIGestureRecognizerStateChanged:
        case UIGestureRecognizerStateEnded: {
            CGPoint translation = [pan translationInView:view];
            CGRect frame = originalFrame;
            frame.origin.y += translation.y;
            view.frame = frame;
        } break;
    }
}


Removing this line may well help you:

newLocation.y = newLocation.y + (self.frame.size.height - locationInsideBox.y) / 2;


What you're trying to accomplish is really nothing more than a scrollable view, so I would recommend using a UIScrollView.

Put your UIView in a UIScrollView with transparent background, and place the UIScrollView on top of your textbox. Set the correct contentSize and you're good to go.


use a uiview animation block to update the frame of the sliding view corresponding with the touch point recieved. set the animation blocks duration to something really short like .01 or lower.


I'd recommend splitting the issue in two:

  • Implement the view which has the text box at the bottom - You will just have to implement your own custom view/view controller.
  • Add your view as a subview of a UIScrollView.

This is a good tutorial which demonstrates proper initialization of UIScrollView and embedding content in it.

Custom views/controllers are a bit of a broader subject :)

0

精彩评论

暂无评论...
验证码 换一张
取 消