krakax
krakax

Reputation: 79

Appcelerator alloy views slide

I am using the Alloy MVC framework over Titanium and want to make a slideshow between views. When I swipe on the screen, I want to display the next/previous view with a slide effect from right to left or left to right. I am using this code:

A tab in my index.xml:

<Tab title="Bilan" icon="KS_nav_ui.png">
    <Window title="Bilan" id="bilanTab" onSwipe="doBilanSwipe">
    </Window>
</Tab>

The question view dynamically added and filled inside bilanTab:

<Alloy>
<Collection src="ReponsePossible">
<View id="questionContainer" class="container">
    <Label id="questionText" />
    <Button id="buttonNextQuestion">Question suivante</Button>
</View>
</Alloy>

and my two functions (3 with prevQuestion not printed here) inside index.js controller:

var previousQuestion;
var nextQuestion;

function doBilanSwipe(e){
    if (e.direction == 'left'){
        nextQuestion();
    }
    else if (e.direction == 'right'){
        prevQuestion();
    }
}



function nextQuestion(){
    if (questionsCurrentIndex < questions.length-1){
        questionsCurrentIndex++;
        $.previous = previousQuestion;
        $.next = Alloy.createController('question', questions.at(questionsCurrentIndex));
        nextQuestion = $.next;
        $.next.questionContainer.left = 320;
        $.bilanTab.add($.next.questionContainer);
        $.next.questionContainer.animate({left:0, duration:200});
        $.previous.questionContainer.animate({left:-320, duration:200},function(){
            $.previous = previousQuestion;
            $.next = nextQuestion;
            $.bilanTab.remove($.previous.questionContainer);
            previousQuestion = $.next;
            $.previous.destroy();
        });
    }
}

My problem is that first animation (first view moving to the left) is ok but after that, the next view just appear without any animation.

Could someone help? Thanks!

Upvotes: 1

Views: 4736

Answers (1)

Josiah Hester
Josiah Hester

Reputation: 6095

There is already the Titanium.UI.ScrollableView that does this exact thing, for all platforms.

Use it in Alloy like this:

<Alloy>
    <Window id="win">
        <ScrollableView id="scrollableView" showPagingControl="true">
            <View id="view1" backgroundColor="#123" />
            <View id="view2" backgroundColor="#246" />
            <View id="view3" backgroundColor="#48b" />
        </ScrollableView>
    </Window>
</Alloy>

You can dynamically add views to it inside the controller like this:

$.scrollableView.addView(Ti.UI.createView({ // your custom attributes here});

Upvotes: 6

Related Questions