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

孤人 提交于 2019-12-11 07:47:34

问题


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:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/504843/iphone-sdk-after-a-certain-number-of-characters-entered-the-animation-just-won

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!