Search code examples
iphoneobjective-ccore-animation

iPhone SDK: After a certain number of characters entered, the animation just won't load


Okay, this is the code:

[lblMessage setText: txtEnter.text];
[lblMessage sizeToFit];

scrollingTextView.contentSize = lblMessage.frame.size;
float width = (lblMessage.frame.size.width) + (480);

[UIView beginAnimations:@"pan" context:nil];
[UIView setAnimationDuration:durationValue];
[UIView setAnimationRepeatCount:5];
scrollingTextView.contentOffset = CGPointMake(width,0);
[UIView commitAnimations];

//The scrolling text view is rotated.
scrollingTextView.transform = CGAffineTransformMakeRotation (3.14/2);

[self.navigationController setNavigationBarHidden:YES];
btnChange.transform = CGAffineTransformMakeRotation (3.14/2);

I have the user enter in some text, press a button and then a label is replaced with the text, turned 90 degrees in a scrollview on a page.

After a certain number of characters, for example say 20.. the animation just won't load. I can go back down until the animation will run.

Any ideas on where I am going wrong, or a better way of storing the text etc etc ?


Solution

  • Core Animation animations are performed on a separate thread. When you enclose the change in contentOffset in a beginAnimations / commitAnimations block, that change will be animated gradually. The scrolling text view rotation that occurs next, outside of the animation block, will be performed instantly. Since both are interacting with the same control on different threads, it's not surprising that you're getting weird behavior.

    If you want to animate the rotation of the text in the same way as the contentOffset, move that line of code to within the animation block.

    If you want to have the rotation occur after the offset change animation has completed, set up a callback delegate method. You can use code in the beginning of your animation block similar to the following:

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(contentOffsetAnimationHasFinished:finished:context:)];
    

    which requires you to implement a delegate method like the following:

    - (void)contentOffsetAnimationHasFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context;
    {
    // Do what you need to, now that the first animation has completed
    }
    

    EDIT (2/6/2009): I just created a simplified version of your application, using only the sideways text scrolling, and find no problem with the animation on the device with any number of characters. I removed all extraneous calls to layout the buttons, etc., and only animate the text. Rather than apply the rotation transform to the scroll view every time you click the button, I have it start rotated and stay that way.

    I thought it might be a layer size issue, as the iPhone has a 1024 x 1024 texture size limit (after which you need to use a CATiledLayer to back your UIView), but I was able to lay out text wider than 1024 pixels and still have this work.

    A full Xcode project demonstrating this can be downloaded here. I don't know what your issue is, but it's not with the text animating code you present here.