如何在nativescript中从本机ios访问UIScrollview委托?
Iam使用nativescript angular应用程序运行android和ios应用程序。我想访问ios的scrollview代理,我从本机ios访问了scrollview代理,代码如下。但它不工作: My scrollview委托实现类:如何在nativescript中从本机ios访问UIScrollview委托?,nativescript,nativescript-angular,Nativescript,Nativescript Angular,Iam使用nativescript angular应用程序运行android和ios应用程序。我想访问ios的scrollview代理,我从本机ios访问了scrollview代理,代码如下。但它不工作: My scrollview委托实现类: // import {Injectable, EventEmitter,Output} from "@angular/core"; @ObjCClass(UIScrollViewDelegate) export class FoodScrollDele
// import {Injectable, EventEmitter,Output} from "@angular/core";
@ObjCClass(UIScrollViewDelegate)
export class FoodScrollDelegate extends NSObject implements UIScrollViewDelegate
{
public static ObjCProtocols = [UIScrollViewDelegate];
// @Output() yvalue:EventEmitter<any>=new EventEmitter();
private _originalDelegate:UIScrollViewDelegate;
public getY:Number=0;
constructor()
{
super();
}
public static initWithOriginalDelegate(originalDelegate:UIScrollViewDelegate):FoodScrollDelegate{
console.log("Called Method");
let delegate=<FoodScrollDelegate>FoodScrollDelegate.new();
delegate._originalDelegate=originalDelegate;
return delegate;
}
}
在i内部实现了委托方法。但是它正在工作
objc类
是objc协议
的替代语法。你可能不需要同时做这两件事
加载事件后会附加默认代理,因此您的代理可能会再次被覆盖。您必须扩展ScrollView并覆盖附件
方法
class MyScrollView extends ScrollView {
protected attachNative() {
(<any>ScrollView.prototype).attachNative.call(this);
if (isIOS) {
(<any>this)._delegate = FoodScrollDelegate.initWithOriginalDelegate((<any>this)._delegate);
this.nativeViewProtected.delegate = (<any>this)._delegate;
}
}
}
现在,您应该能够在HTML中使用MyScrollView
而不是ScrollView
,这将覆盖默认委托。如果您想覆盖应用程序中使用的所有ScrollView的委托,只需在iOS上有条件地覆盖ScrollView
的原型链即可
(<any>ScrollView.prototype).originalAttachNative = (<any>ScrollView.prototype).attachNative;
(<any>ScrollView.prototype).attachNative = function () {
this.originalAttachNative();
const newDelegate = FoodScrollDelegate.initWithOriginalDelegate(this._delegate);
this._delegate = newDelegate;
this.nativeViewProtected.delegate = newDelegate;
};
TS
onDragEnd(event) {
console.log(event.decelerate);
}
委托方法仅在FoodScrollDelegate类中工作。我希望这些方法在html相关的组件类中工作。您所要做的就是触发事件并从组件中侦听它。好的,我将编辑我的问题,我在组件类中被称为委托方法的内容。Hai manoj我被提供了我的组件类请告诉我如何在类中调用委托方法?正如我前面提到的,您可以在ScrollView上触发一个事件,并从组件中侦听相同的事件。
registerElement("MyScrollView", () => MyScrollView);
(<any>ScrollView.prototype).originalAttachNative = (<any>ScrollView.prototype).attachNative;
(<any>ScrollView.prototype).attachNative = function () {
this.originalAttachNative();
const newDelegate = FoodScrollDelegate.initWithOriginalDelegate(this._delegate);
this._delegate = newDelegate;
this.nativeViewProtected.delegate = newDelegate;
};
scrollViewDidEndDraggingWillDecelerate(scrollView: UIScrollView, decelerate: boolean) {
const owner = (<WeakRef<ScrollView>>(<any>this._originalDelegate)._owner).get();
if (owner) {
owner.notify({
object: owner,
eventName: "dragEnd",
decelerate: decelerate
});
}
}
(dragEnd)="onDragEnd($event)"
onDragEnd(event) {
console.log(event.decelerate);
}