Difficulty using WatchUi.animate()

Former Member
Former Member

I'm trying to animate a chunk of text to simulate scrolling where I have some text with different fonts and colors.


My approach to this was:
1. Create an array to put my Text objects in my view class
2. When loading the text, add my text objects to that array (https://prnt.sc/12qlv31)

var foodName = new WatchUi.Text({
    :text => foodFullName,
    :color => 0xFFFFFF,
    :font => Gfx.FONT_TINY,
    :justification => Gfx.TEXT_JUSTIFY_VCENTER+Gfx.TEXT_JUSTIFY_LEFT,
    :locX => 40,
    :locY => 75 + j*50 
});
mealText.add(foodName);


var foodQuantity = new WatchUi.Text({
    :text => meals[screenStatus.get("meal")].get("food")[j].get("quantity") + "g",
    :color => 0x999999,
    :font => Gfx.FONT_XTINY,
    :justification => Gfx.TEXT_JUSTIFY_VCENTER+Gfx.TEXT_JUSTIFY_LEFT,
    :locX => 40,
    :locY => 90 + j*50 
});
mealText.add(foodQuantity);

3. When the screen is tapped, this function is called 

function scrollAnimation(){
    for(var i = 0; i < mealText.size(); i++){
        WatchUi.animate(mealText[i], :locY, WatchUi.ANIM_TYPE_LINEAR, mealText[i].locX, mealText[i].locY + 200, 0.25, method(:fx) );
    }
}

4. And finally I'm drawing those objects in the onUpdate()

for(var i = 0; i < mealText.size(); i++){
    mealText[i].draw(dc);
}

What am I doing wrong? The elements are rendered but there is no animations.

  • This code (built from your snippets above) works just fine for me.

    import Toybox.Application;
    import Toybox.Lang;
    import Toybox.WatchUi;
    import Toybox.Graphics;
    import Toybox.WatchUi;
    import Toybox.Lang;
    
    class TestDelegate extends WatchUi.BehaviorDelegate {
    
        hidden var _view as TestView;
        
        function initialize(view) {
            BehaviorDelegate.initialize();
            _view = view;
        }
    
        function onSelect() as Boolean {
            _view.scrollAnimation();
            return true;
        }
    
    }
    
    class TestView extends WatchUi.View {
    
        function initialize() {
            View.initialize();
        }
        
        hidden var mealText as Array<Drawable> = [] as Array<Drawable>;
    
        // Load your resources here
        function onLayout(dc as Dc) as Void {
            mealText = [] as Array<Drawable>;
    
            var foodName = new WatchUi.Text({
                :text => "Blah Blah Blah",
                :color => 0xFFFFFF,
                :font => Graphics.FONT_TINY,
                :justification => Graphics.TEXT_JUSTIFY_VCENTER | Graphics.TEXT_JUSTIFY_LEFT,
                :locX => 40,
                :locY => 10 
            });
            mealText.add(foodName);
            
            
            var foodQuantity = new WatchUi.Text({
                :text => "More More More",
                :color => 0x999999,
                :font => Graphics.FONT_XTINY,
                :justification => Graphics.TEXT_JUSTIFY_VCENTER | Graphics.TEXT_JUSTIFY_LEFT,
                :locX => 40,
                :locY => 40 
            });
            
            mealText.add(foodQuantity);
        }
    
        function scrollAnimation() as Void {
            for(var i = 0; i < mealText.size(); i++){
                WatchUi.animate(mealText[i], :locY, WatchUi.ANIM_TYPE_LINEAR, mealText[i].locY, mealText[i].locY + 200, 3.0, method(:fx) );
            }
        }
        
        function fx() as Void {
            mealText[0].locY = 10;
            mealText[1].locY = 40;
        }
        
    
        function onUpdate(dc as Dc) as Void {
            dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_BLACK);
            dc.clear();
    
            for(var i = 0; i < mealText.size(); i++) {
                mealText[i].draw(dc);
            }
        }
    }
    
    class TestApp extends Application.AppBase {
    
        function initialize() {
            AppBase.initialize();
        }
    
        function getInitialView() as Array<Views or InputDelegates>? {
            var view = new TestView();
            return [ view, new TestDelegate(view) ] as Array<Views or InputDelegates>;
        }
    
    }
    

    What app type are we dealing with here?

    IMO, if you are trying to scroll a multiline block of text, I think it would be better to not use multiple WatchUi.Text objects. Doing so is a bunch of memory and you'd be better suited to using an array of strings or a single TextArea. I'd probably not use WatchUi.animate() either.

  • How would you scroll through text then? i am also trying to figure it out for a smooth experience.. is there any way to show text and scroll it like you scroll lists or menus? thanks in advance!

  • would also like the answer to this