【发布时间】:2014-09-26 07:54:28
【问题描述】:
我正在创建一种网格视图,您可以在其中添加一些小部件,并根据需要通过拖放来重新排列它们。 我现在的网格视图是 UIscroll 视图,小部件是 UIview 的子类。
这是您可以使用的网格示例 当我拖动一个小部件时,如果小部件靠近屏幕底部,我希望能够使滚动视图向下。 现在,我的小部件有一个 UIPanGestureRecognizer 以及以下代码:
func enterDragMode(recognizer:UIPanGestureRecognizer) {
if (recognizer.state == UIGestureRecognizerState.Ended) || (recognizer.state == UIGestureRecognizerState.Cancelled) {
gridNotificationCenter.postNotificationName("WidgetDragEnded", object: self)
// Notify the grid and drop the widget here
} else {
var translation = recognizer.translationInView(self.superview!)
var newPoint = self.center
newPoint.x += translation.x
newPoint.y += translation.y
if (CGRectContainsRect(moveDownRect, self.frame)) {
var scrollview = self.superview as? UIScrollView
if (scrollview != nil) {
// need to find correct visible rect here
res!.scrollRectToVisible(visibleRect, animated: true)
}
}
recognizer.setTranslation(CGPointZero, inView: self.superview)
}
}
但我觉得我没有使用正确的类来正确处理这个问题,因为当我只是拿着视图而不移动它时不会触发回调。有没有更好的办法?
【问题讨论】:
-
这对我来说是 ui collection view 的尖叫。由于它是 uiscrollview 的子类,因此它具有相同的属性以及布局的额外好处。无论如何,我假设这段代码在小部件类中? moveDownRect 是从哪里来的?
-
Move downRect 是widgetview 内部的一个常量rec,是的,这段代码来自widget view。我没有使用collectionview,因为所有小部件的大小可能不一样,也可以自行调整大小。
-
您是否将滚动视图的内容大小设置为大于其框架?是否达到了设置的内容偏移代码?
-
我不确定你的意思?
-
如果你在 scrollview!.setContentOffset(contentOffset, animated: true) 行上设置断点,它会停在那里吗?为了滚动视图滚动,它们需要一个大于其框架的内容大小..您过去必须在代码中执行此操作,即滚动视图 setcontentSize:您现在可以在界面生成器中执行此操作,我认为使用自动布局
标签: ios swift uiscrollview drag-and-drop