类似 iOS 中的 Control Center 跟随手指向上滑动 UIView
Slide up UIView following finger similar to Control Center in iOS
我有一个 UITableView
位于主 UIViewController
的底部,它目前只显示前两行。为了显示其余部分,我不希望它滚动,我希望用户能够 "pull" 视图向上显示额外的 4 行(然后可以向下滑动它以 "push" 返回到原来的位置),我希望 "pull" 类似于控制中心在 iOS 中的工作方式。它所拥有的 spring 真的很棒。
看来我可以添加一个 UIPanGestureRecognizer
来进行拉动:
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
pan.maximumNumberOfTouches = pan.minimumNumberOfTouches = 1;
[self addGestureRecognizer:pan];
- (void)pan:(UIPanGestureRecognizer *)aPan; {
CGPoint currentPoint = [aPan locationInView:self];
[UIView animateWithDuration:0.01f
animations:^{
CGRect oldFrame = _viewToChange.frame;
_viewToChange.frame = CGRectMake(oldFrame.origin.x, currentPoint.y, oldFrame.size.width, ([UIScreen mainScreen].bounds.size.height - currentPoint.y));
}];
}
Ref this question.
这样做会一些工作,但是我的UITableView
会在您拉起它时闪烁并最终消失。
也没有spring,也没有办法设置一个"max",这样你就不能将视图拉过某个点。
有人知道如何实现吗?
这就是我写 pan:
的方式。 t
是 tableView(最初禁用用户交互)。代码中的 40 是为了在 pan
中看起来更逼真。 200 是我使用的最大值,60 是最小值。我直接将 t
添加到高度为 60 的 tableview 并通过动画增加高度。
- (void)pan:(UIPanGestureRecognizer *)aPan; {
CGPoint currentPoint = [aPan locationInView:self.view];
CGRect fr = t.frame;
if ((currentPoint.y - fr.origin.y) < 40 && (fr.size.height <= 200) )
{
float nh = (currentPoint.y >= self.view.frame.size.height - 200) ? self.view.frame.size.height-currentPoint.y : 200;
if (nh < 60) {
nh = 60;
}
[UIView animateWithDuration:0.01f animations:^{
[t setFrame:CGRectMake(0, self.view.frame.size.height-nh, t.frame.size.width, nh)];
}];
if (nh == 200) {
[t setUserInteractionEnabled:YES];
}
else
{
[t setUserInteractionEnabled:NO];
}
}
}
或者没有平底锅;使用 tableview 的 scrollview 委托方法,当 tableview 在关闭状态下开始拖动时,打开大模式,当 tableview 滚动到顶部时向下滑动
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"scroll");
if (scrollView.contentOffset.y == 0) {
[UIView animateWithDuration:0.2f animations:^{
[scrollView setFrame:CGRectMake(0, self.view.frame.size.height-200, t.frame.size.width, 200)];
}];
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y == 0) {
[UIView animateWithDuration:0.2f animations:^{
[scrollView setFrame:CGRectMake(0, self.view.frame.size.height-60, t.frame.size.width, 60)];
}];
}
}
我有一个 UITableView
位于主 UIViewController
的底部,它目前只显示前两行。为了显示其余部分,我不希望它滚动,我希望用户能够 "pull" 视图向上显示额外的 4 行(然后可以向下滑动它以 "push" 返回到原来的位置),我希望 "pull" 类似于控制中心在 iOS 中的工作方式。它所拥有的 spring 真的很棒。
看来我可以添加一个 UIPanGestureRecognizer
来进行拉动:
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
pan.maximumNumberOfTouches = pan.minimumNumberOfTouches = 1;
[self addGestureRecognizer:pan];
- (void)pan:(UIPanGestureRecognizer *)aPan; {
CGPoint currentPoint = [aPan locationInView:self];
[UIView animateWithDuration:0.01f
animations:^{
CGRect oldFrame = _viewToChange.frame;
_viewToChange.frame = CGRectMake(oldFrame.origin.x, currentPoint.y, oldFrame.size.width, ([UIScreen mainScreen].bounds.size.height - currentPoint.y));
}];
}
Ref this question.
这样做会一些工作,但是我的UITableView
会在您拉起它时闪烁并最终消失。
也没有spring,也没有办法设置一个"max",这样你就不能将视图拉过某个点。
有人知道如何实现吗?
这就是我写 pan:
的方式。 t
是 tableView(最初禁用用户交互)。代码中的 40 是为了在 pan
中看起来更逼真。 200 是我使用的最大值,60 是最小值。我直接将 t
添加到高度为 60 的 tableview 并通过动画增加高度。
- (void)pan:(UIPanGestureRecognizer *)aPan; {
CGPoint currentPoint = [aPan locationInView:self.view];
CGRect fr = t.frame;
if ((currentPoint.y - fr.origin.y) < 40 && (fr.size.height <= 200) )
{
float nh = (currentPoint.y >= self.view.frame.size.height - 200) ? self.view.frame.size.height-currentPoint.y : 200;
if (nh < 60) {
nh = 60;
}
[UIView animateWithDuration:0.01f animations:^{
[t setFrame:CGRectMake(0, self.view.frame.size.height-nh, t.frame.size.width, nh)];
}];
if (nh == 200) {
[t setUserInteractionEnabled:YES];
}
else
{
[t setUserInteractionEnabled:NO];
}
}
}
或者没有平底锅;使用 tableview 的 scrollview 委托方法,当 tableview 在关闭状态下开始拖动时,打开大模式,当 tableview 滚动到顶部时向下滑动
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"scroll");
if (scrollView.contentOffset.y == 0) {
[UIView animateWithDuration:0.2f animations:^{
[scrollView setFrame:CGRectMake(0, self.view.frame.size.height-200, t.frame.size.width, 200)];
}];
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y == 0) {
[UIView animateWithDuration:0.2f animations:^{
[scrollView setFrame:CGRectMake(0, self.view.frame.size.height-60, t.frame.size.width, 60)];
}];
}
}