views:

2278

answers:

3

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 ?

+1  A: 

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.

Brad Larson
See it's absolutely fine in the Simulator, but on the iphone it just doesn't appear above a certain amount of characters, like it's too much.
Domness
Okay, I've tried your view and there definitley is a limit. My text is much much larger and therefore reaches this limit very quickly. A part from downsizing the text size (which i can't afford to do) im not sure how to go about this. UNless there's another way to complete this action.
Domness
Alright, if I change the text size to a truly epic scale (200 pt), I can get the display to fail if the width of the label goes over 2048 pixels. This looks like a hard texture limit, and you won't be able to draw a single layer-backed view larger than this size.
Brad Larson
You could do a CATiledLayer for your text content's backing layer, but I don't think that will animate as smoothly as you would like (I think it will visibly page in tiles of the view as needed).
Brad Larson
My recommendation would be to split the NSString for the input text into smaller manageable chunks and programmatically generate a series of UILabels that you animate along a line. You can't place them within a scroll view, but you could just have a normal view as a window into the moving views.
Brad Larson
A: 

Right, this code is working fine in the simulator, and works fine until i enter more than say 20 characters in txtEnter.text:

- (IBAction)updateMessage:(id)sender
{


    //Animation coding

    //Put the message in a resize the label
    [lblMessage setText: txtEnter.text];
    [lblMessage sizeToFit];

    //Resize the scrolliew and change the width.
    scrollingTextView.contentSize = lblMessage.frame.size;
    float width = (lblMessage.frame.size.width) + (480);
    scrollingTextView.transform = CGAffineTransformMakeRotation (3.14/2);

    //Begin the animations
    [UIView beginAnimations:@"pan" context:nil];
    [UIView setAnimationDuration:durationValue];
    [UIView setAnimationRepeatCount:5];

    //Start the scrolling text view to go across the screen
    scrollingTextView.contentOffset = CGPointMake(width,0);
    [UIView commitAnimations];


    //General hiding and showing points.
    [txtEnter resignFirstResponder];
    [btnChange setHidden:NO];
    [txtEnter setHidden:YES];
    [btnUpdate setHidden:YES];
    [lblSpeed setHidden:YES];
    [lblBackground setHidden:YES];
    [backgroundColourControl setHidden:YES];
    [speedSlider setHidden:YES];
    [scrollingTextView setHidden:NO];
    [backgroundImg setHidden:NO];
    [toolbar setHidden:YES];

    [self.navigationController setNavigationBarHidden:YES animated:YES];

    //Depending on the choice from the segment control, different colours are loaded
    switch([backgroundColourControl selectedSegmentIndex] + 1) 
    { 
     case 1: 
      [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
      break; 
     case 2: 
      [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:YES];
      break; 
     default: break; 
    }  

    btnChange.transform = CGAffineTransformMakeRotation (3.14/2);

}

I've tried your method Brad, but can't seem to get the (void) section to work properly.

What my app does its fill the label with a message and then rotates them all to act like it's in landscape mode. Then what it does it scroll the label within a scrollview to act like a scrolling message across the screen.

Domness
If by the (void) section, you mean the delegate method, it will be called after your animation is completed, if you use the exact code above. However, it looks like your animation might take a while to complete, given the repeatCount.
Brad Larson
Speaking of which, I wonder if your animations aren't being piled on top one another, since you probably aren't giving them enough time to complete between keystrokes. Maybe try inserting [UIView setAnimationBeginsFromCurrentState:YES] at the top of your animation block.
Brad Larson
Hmm.. I've tried that.It's funny, I tried again, and on the very last 2 pixels of the iPhone screen where the text is meant to start scrolling in, it glitches and flicker on the 2 rows of pixels before vanishing after around 4 seconds.
Domness
But again, it's only over like 20 characters worth of words. ON the simulator it's fine, just not working properly on the iPhone.
Domness
I've edited my response with a link to an Xcode project that shows that the text animation code above works properly. Your problem must be elsewhere in the many things that are going on in this method.
Brad Larson
A: 

Bump! It's really not going anywhere at the moment.

Domness