我希望在一些标签中使用drag'n'drop功能,这些标签在nativescript应用程序中以绝对布局定位。甚至可以使用拖拽来将它们移动到新的绝对位置吗?如果是的话,你能解释一下实现这个目标的方法吗?
提前谢谢。
答案 0 :(得分:4)
经过一番研究后,我发现您可以通过以下步骤实现这一目标:
示例xml:
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
<AbsoluteLayout>
<Label left="10" top="10" text="drag me" id="dragLabel" />
</AbsoluteLayout>
</Page>
示例js代码:
var viewModule = require("ui/core/view");
var gestures = require("ui/gestures");
var absoluteLayout = require("ui/layouts/absolute-layout");
function pageLoaded(args) {
var page = args.object;
var dragLabel = viewModule.getViewById(page, "dragLabel");
var observer = dragLabel.observe(gestures.GestureTypes.pan, function (panGestureEventData) {
var deltaX = panGestureEventData.deltaX;
var deltaY = panGestureEventData.deltaY;
absoluteLayout.AbsoluteLayout.setTop(this, deltaY);
absoluteLayout.AbsoluteLayout.setLeft(this, deltaX);
}, dragLabel);
};
此外,当前版本(1.2.x)中的nativescript不支持手势中的状态。在平移手势中,国家真的有必要知道。这是你可以做的(在ios中),所以你可以获得状态。编辑文件/tns_modules/ui/gestures/gestures.ios.js并使用以下内容更改函数_getPanData:
function _getPanData(args, view) {
var recognizer = args.ios;
var state = "unknown";
switch(recognizer.state) {
case UIGestureRecognizerStateBegan:
state = "began";
break;
case UIGestureRecognizerStateChanged:
state = "changed";
break;
case UIGestureRecognizerStateEnded:
state = "ended";
break;
case UIGestureRecognizerStateCancelled:
state = "cancelled";
break;
case UIGestureRecognizerStateFailed:
state = "failed";
break;
}
return {
type: args.type,
view: args.view,
ios: args.ios,
android: undefined,
deltaX: recognizer.translationInView(view).x,
deltaY: recognizer.translationInView(view).y,
state: state
};
}
然后,您可以使用州字段访问状态:
var observer = dragLabel.observe(gestures.GestureTypes.pan, function (panGestureEventData) {
// ... panGestureEventData.state has on of the above values...
});