4

我正在尝试制作一个 UIView/UIControl,人们可以向上拖动并显示一个文本框,然后向下拖动以隐藏它。但是,我还没有找到一种方法来使这种“流动”——它似乎总是停在随机的地方,并且不允许更多的移动。目前我正在使用 UIView 作为视图的顶部,这是当前代码:

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

 UITouch *touch = [touches anyObject];

 if ([touch view] == topMenuView) {
  CGPoint location = [touch locationInView: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;
 }
}

任何帮助将非常感激!

4

5 回答 5

1

我会使用平移手势识别器。下面的代码将简单地随着用户的手指上下移动视图。如果你想限制它向上移动的距离,让它快速放置或有你需要添加到它的动力。

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;
    }
}
于 2010-10-16T23:30:24.220 回答
0

删除此行可能会对您有所帮助:

newLocation.y = newLocation.y + (self.frame.size.height - locationInsideBox.y) / 2;
于 2010-10-11T14:17:46.477 回答
0

你想要完成的实际上只不过是一个可滚动的视图,所以我建议使用 UIScrollView。

将 UIView 放在具有透明背景的 UIScrollView 中,并将 UIScrollView 放在文本框的顶部。设置正确的 contentSize 就可以了。

于 2010-10-11T14:22:37.637 回答
0

使用 uiview 动画块来更新与接收到的触摸点对应的滑动视图的帧。将动画块的持续时间设置为非常短的值,例如 0.01 或更低。

于 2010-10-16T02:41:51.697 回答
0

我建议将问题一分为二:

  • 实现底部有文本框的视图 - 您只需要实现自己的自定义视图/视图控制器。
  • 将您的视图添加为 UIScrollView 的子视图。

是一个很好的教程,它演示了 UIScrollView 的正确初始化并在其中嵌入内容。

自定义视图/控制器是一个更广泛的主题:)

于 2010-10-16T11:37:04.717 回答