Scrollview Events

Hello,

I was wondering if there was a way to get a callback when a Scrollview is done dragging so that I can center it on the nearest child.

Something along the lines of:

this.entity.scrollview.on('doneDragging',this.centerScrollvew,this);

The only event for Scrollviews that I can find in the forums or online is ‘set:scroll’ but I just need to fire a method when the dragging is done.

Thanks,
Justin

There is but it’s not public API.

You can listen on the drag helpers events for drag:end

Thank you for your response!
Could you give an example of how to do that? I am not very clear what you mean by listen on the drag helpers for the scrollview.

Something like this?

this.entity.scrollview._contentDragHelper.on(‘drag:end’,this.centerScrollview,this);

Actually, just trying this myself and looking at the code, it’s not as easy as that unfortunately. Doesn’t feel like there’s a good way of doing this.

The closest I’ve got is this that uses the private API that is least likely to change: https://playcanvas.com/editor/scene/1155593

var DragEnd = pc.createScript('dragEnd');

// initialize code called once per entity
DragEnd.prototype.initialize = function() {
    this._draggingLastFrame = this.entity.scrollview._isDragging();
};

// update code called every frame
DragEnd.prototype.update = function(dt) {
    var dragging = this.entity.scrollview._isDragging();
    if (this._draggingLastFrame && !dragging) {
        console.log('drag end');
    }
    
    this._draggingLastFrame = dragging;
};
2 Likes

Hey thank you so much for getting a solution worked up for me! I really appreciate that!