Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Specify views dynamically for transitions

Hello all, I have a 'book' scenerio where I'm trying to track my previous and next pages (views) using dynamic variables. It works fine if I specify a view to open statically but I need be able to do this dynamically.

Seems the 'view' attribute only wants 'real' names of views and crashes when I try to use variables like below…

curr = 0;
nextBtn.addEventListener('click', function(e)
{
page = 'view'+curr;
view.animate({view:page,transition:Ti.UI.iPhone.AnimationStyle.CURL_DOWN});
curr++;
});

THX!

— asked May 19th 2010 by Kelly Redd
  • animate
  • views
0 Comments

5 Answers

  • try this

    page = eval('view'+curr)
    

    or define an array of pages and access what you need by id

    var pages_arr = [];
    
    page= Titanium.UI.createView(....
    
    pages_arr.push(page)
    
    ....
    
    
    curr= 5;
    curr_page = pages_arr[5]
    
    view.animate({view: curr_page....
    

    Let me know which version works, if any.. :)
    ~~~

    — answered May 19th 2010 by Dan Tamas
    permalink
    0 Comments
  • Hey thanks Tamas, actually got it figured out and was on my way to post the solution…just like you suggested, an array was the right way to go…

    Thx

    var pageArr = [
        {page:page0},
        {page:page1},
        {page:page2},
        {page:page3}
    ];
    
    prevBtn.addEventListener('click', function(e)
    {
      current_page--;
      view.animate({view:pageArr[current_page].page,transition:Ti.UI.iPhone.AnimationStyle.CURL_DOWN});
    });
    
    nextBtn.addEventListener('click', function(e)
    {
      current_page++;
      view.animate({view:pageArr[current_page].page,transition:Ti.UI.iPhone.AnimationStyle.CURL_DOWN});
    });
    
    — answered May 19th 2010 by Kelly Redd
    permalink
    0 Comments
  • Can you explain to me how you exactly did it?
    Im trying to achieve sort of the same.

    I have an array of images for the pages.
    http://pastie.org/1281266

    I want to dynamically go through these with a CURL_UP and CURL_DOWN Animation in a swipe function.

    should i define every "page" in a view myself or can I create a for loop and make alot of views and then push these to an array?

    — answered November 8th 2010 by Patrick van Zadel
    permalink
    0 Comments
  • Can you still show me an example code?
    Im trying to do this, but it shows my last array item first then the animation does work.
    And I have trouble adding text to the views.

    Can you show how you did it?

    — answered November 23rd 2010 by Patrick van Zadel
    permalink
    0 Comments
  • Hi,
    I have the same trouble. Here my code. Every click shows the same image:

    function createKugelView() {
        var v = Ti.UI.createView({
        });
        var kTitle = Ti.UI.createLabel({
            text : "Tests with some new Methods & Materials\n\n12/12/10 — 6:19pm",
            top : 100,
            height : 'auto',
            color : '#aaa',
            font : {
                fontSize : 18,
                fontFamily : 'QuicksandBold-Regular'
            },
            left : 5
        });
        var imageView = Titanium.UI.createView({
            height : '36%',
            width : '100%',
            bottom : 30
        });
        var images = [];
        var total = 10;
        var i;
        for(i = 0; i < total; i++) {
            images.push(Ti.UI.createView({
                backgroundImage : Ti.Filesystem.resourcesDirectory + 'assets/k' + i + '.png',
                height : '100%',
                width : '100%',
                borderRadius : 6
            }));
        }
        Ti.API.log(images.length);
    
        for(i = 0; i < total - 1; i++) {
            images[i].addEventListener('click', function() {
                imageView.animate({
                    view : images[i + 1],
                    transition : Ti.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT
                });
            });
        }
        images[total - 1].addEventListener('click', function() {
            imageView.animate({
                view : images[0],
                transition : Ti.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT
            });
        });
    
        for(i = total - 1; i >= 0; i--) {
            imageView.add(images[i]);
        }
        v.add(kTitle);
        v.add(imageView);
        return v;
    };
    

    ??? Whats going wrong?

    — answered October 24th 2011 by Rainer Schleevoigt
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.