gpt4 book ai didi

iphone - CocoaTouch 中的拉菜单

转载 作者:行者123 更新时间:2023-12-03 19:23:00 26 4
gpt4 key购买 nike

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

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

任何帮助将不胜感激!

最佳答案

我会使用平移手势识别器。以下代码将简单地随着用户的手指上下移动 View 。如果您想限制它向上移动的距离,请让它快速放置或具有您需要添加的动量。

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;
}
}

关于iphone - CocoaTouch 中的拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3865066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com