响应手指拖动更改UIView的高度 [英] Changing height of UIView in response to finger drag
问题描述
我知道标题有点混乱,所以我会尽力将其解释清楚.我有一个固定宽度为100且高度从0开始的UIView.我想要做的就是从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屋!