改变UIView的高度以响应手指拖动 [英] Changing height of UIView in reponse to finger drag
问题描述
我知道标题有点令人困惑所以我会尽力解释它。我有一个固定宽度为100的UIView,以及从0开始的可变高度。我想要做的是将手指从UIView的底部拖到屏幕的顶部,以及UIView改变它的高度/延伸到我的手指的位置。如果那仍然复杂只是想象它是从一些东西下拉出来的纸条。
I understand that the title is a bit confusing so I'll try my best to explain it well. I have a UIView that has a fixed width of 100, and a variable height that starts at 0. What I want to be able to do is drag my finger from the base of the UIView, towards the top of the screen, and the UIView changes its height/extends to the position of my finger. If that's still to complicated just imagine it as a strip of paper being pulled out from under something.
如果你能提供帮助,那将是非常好的。我认为它不应该太难,但我只是一个初学者,我能理解,如果我没有解释得好!
If you can help, that would be really great. I don't think it should be too hard, but I'm only a beginner and I can understand if I haven't explained it well!
推荐答案
这应该是直接的 UIPanGestureRecognizer
和一点点数学。要将视图更改为正确的框架(我使用名称 _viewToChange
,请稍后将其替换为您的视图),只需添加:
This should be straight-forward with a UIPanGestureRecognizer
and a little math. To change the view to the correct frame (I'm using the name _viewToChange
, so replace that with your view later), simply add:
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
pan.maximumNumberOfTouches = pan.minimumNumberOfTouches = 1;
[self addGestureRecognizer:pan];
到超级视图的 init
方法,并定义方法:
to your init
method for the super view, and define the method:
- (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));
}];
}
这应该会在用户手指移动时为视图设置动画。希望有帮助!
This should animate the view up as the users finger moves. Hope that Helps!
这篇关于改变UIView的高度以响应手指拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!