我知道标题有点困惑,所以我会尽力解释清楚。我有一个固定宽度为 100 的 UIView,以及一个从 0 开始的可变高度。我想要做的是将我的手指从 UIView 的底部拖到屏幕顶部,然后将 UIView改变它的高度/延伸到我手指的位置。如果这仍然很复杂,那就把它想象成从某物下面拉出一条纸条。
如果你能帮忙,那就太好了。我觉得应该不会太难,但我只是初学者,如果我没有解释好,我也能理解!
最佳答案
这应该是简单明了的 UIPanGestureRecognizer
和一点数学。要将 View 更改为正确的框架(我使用名称 _viewToChange
,所以稍后将其替换为您的 View ),只需添加:
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
pan.maximumNumberOfTouches = pan.minimumNumberOfTouches = 1;
[self addGestureRecognizer:pan];
到你的 super View 的init
方法,并定义方法:
- (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));
}];
}
这应该在用户手指移动时使 View 向上动画。希望对您有所帮助!
关于ios - 响应手指拖动更改 UIView 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18427006/