我正在写一个像iBooks这样的书架视图的应用程序。
现在的问题是:当我将图书拖到滚动视图的底部时,我可以将一本书从一个地方拖到another.But,如何使这些情况同时发生:
我知道在Github中有一个AQGridView,但是看起来跳板演示不支持滚动和移动(我已经将scrollEnable设置为YES)。
发布于 2011-07-24 09:13:08
我将给出我的解决方案,但是由于整个事情都很大,所以我将只给您提供相关的片段。
此外,请注意,我使用了一个手势识别器的拖拽(UILongPressGestureRecognizer),因为这是如何启动在我的应用程序中,他的手指按在对象上。因此,每个可以拖动的子视图都有自己的UILongPressGestureRecognizer分配给它,而该识别器的目标/选择器位于管理滚动视图和子视图的另一个类中。
以下是手势识别器的目标:
-(void)dragged:(UILongPressGestureRecognizer *)panRecog
{
if (panRecog.state == UIGestureRecognizerStateBegan)
{
UIView * pannedView = panRecog.view;
dragView = pannedView;
dragView.center = [panRecog locationInView:scrollView];
[scrollView bringSubviewToFront:dragView];
[self startDrag]; // Not important, changes some stuff on screen to show the user he is dragging
return;
}
if (panRecog.state == UIGestureRecognizerStateChanged)
{
int xDelta = dragView.center.x - [panRecog locationInView:scrollView].x;
dragView.center = [panRecog locationInView:scrollView];
[self scrollIfNeeded:[panRecog locationInView:scrollView.superview] withDelta:xDelta];
return;
}
if (panRecog.state == UIGestureRecognizerStateEnded)
{
[self endDrag]; // Not important, changes some stuff on screen to show the user he is not dragging anymore
}
}与你相关的事情:
启动拖动时,
这是密码
-(void)scrollIfNeeded:(CGPoint)locationInScrollSuperview withDelta:(int)xDelta
{
UIView * scrollSuperview = scrollView.superview;
CGRect bounds = scrollSuperview.bounds;
CGPoint scrollOffset = scrollView.contentOffset;
int xOfs = 0;
int speed = 10;
if ((locationInScrollSuperview.x > bounds.size.width * 0.7) && (xDelta < 0))
{
xOfs = speed * locationInScrollSuperview.x/bounds.size.width;
}
if ((locationInScrollSuperview.x < bounds.size.width * 0.3) && (xDelta > 0))
{
xOfs = -speed * (1.0f - locationInScrollSuperview.x/bounds.size.width);
}
if (xOfs < 0)
{
if (scrollOffset.x == 0) return;
if (xOfs < -scrollOffset.x) xOfs = -scrollOffset.x;
}
scrollOffset.x += xOfs;
CGRect rect = CGRectMake(scrollOffset.x, 0, scrollView.bounds.size.width, scrollView.bounds.size.height);
[scrollView scrollRectToVisible:rect animated:NO];
CGPoint center = dragView.center;
center.x += xOfs;
dragView.center=center;
}这件事只做水平滚动,但处理垂直将是相当相似的。它所做的是:
希望这能帮到你。
https://stackoverflow.com/questions/6805825
复制相似问题