1

好的,这是代码:

[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);

我让用户输入一些文本,按下按钮,然后用文本替换标签,在页面上的滚动视图中旋转 90 度。

在一定数量的字符之后,例如说 20.. 动画将无法加载。我可以回去,直到动画运行。

关于我哪里出错的任何想法,或者存储文本等的更好方法等?

4

2 回答 2

1

Core Animation 动画在单独的线程上执行。当您将 contentOffset 的更改包含在 beginAnimations / commitAnimations 块中时,该更改将逐渐动画化。接下来在动画块之外发生的滚动文本视图旋转将立即执行。由于两者都在不同线程上与相同的控件进行交互,因此出现奇怪的行为也就不足为奇了。

如果您想以与 contentOffset 相同的方式为文本的旋转设置动画,请将该行代码移动到动画块内。

如果您希望在偏移更改动画完成后发生旋转,请设置回调委托方法。您可以在动画块的开头使用类似于以下内容的代码:

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

这需要您实现如下委托方法:

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

编辑(2009 年 2 月 6 日):我刚刚创建了您的应用程序的简化版本,仅使用横向文本滚动,并且发现设备上的动画与任意数量的字符都没有问题。我删除了所有无关的调用来布局按钮等,并且只为文本设置动画。我不是每次单击按钮时都将旋转变换应用于滚动视图,而是让它开始旋转并保持这种状态。

我认为这可能是层大小问题,因为 iPhone 有 1024 x 1024 纹理大小限制(之后您需要使用 CATiledLayer 来支持您的 UIView),但我能够布置大于 1024 像素的文本并且仍然有这个工作。

一个完整的 Xcode 项目演示这个可以在这里下载。我不知道您的问题是什么,但这与您在此处提供的文本动画代码无关。

于 2009-02-02T22:46:54.003 回答
0

对,这段代码在模拟器中运行良好,直到我在 txtEnter.text 中输入超过 20 个字符之前都运行良好:

- (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);

}

我已经尝试过你的方法 Brad,但似乎无法让 (void) 部分正常工作。

我的应用程序用一条消息填充标签,然后将它们全部旋转以使其像处于横向模式一样。然后它会在滚动视图中滚动标签,就像在屏幕上滚动消息一样。

于 2009-02-03T14:26:57.083 回答