tags:

views:

496

answers:

3

Hello all,

I want to add some text which is a long text lets say a story and I want it to be presented in a moving up fashion. That is it will be displayed in a box and the text will be moved line by line to show the whole story. So one line gets out of the box from top and other comes from the bottom. Like a HTML marquee behavior.

A: 

I think you want to look at the Scroll View (UIScrollView) in the iPhone SDK.

Depending on your implementation, you may not be able to use it "out of the box" but it should provide the functionality you seem to be describing. I'm not sure it will do the auto-scroll thing but that should be pretty easy to implement... maybe.... But, I'm still confident it could be done.

Frank V
animating the scroll view automatically should be pretty easy
Daniel
Do any one have the code how to scroll the UITextView programatically. Like I tried some in a way having timer, and scrolling the view on timer tick, but the textview wasnt seem like scrolling so I think there is something wrong with my scrolling part.
rkb
+1  A: 

You should look into whether or not UIScrollView's contentOffset property is animatable using UIView animation blocks. If it's not, then you could try using an NSTimer and in its function, either set the contentOffset or call [scrollView scrollRectToVisible:desiredContentFrame animated:YES]; where desiredContentFrame.origin.y is just a little bit lower than what you previously had it at (depending on the scroll speed you want). Then play around with the firing frequency of your timer and the desiredContentFrame.origin.y's delta to see what looks best and has the best performance.

Here's a quick hack showing both ways:

- (void)startAnimTimer
{
    currentRect = CGRectMake(0, 0, 1, 1);
    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(doScroll) userInfo:nil repeats:YES];
}
- (void)startAnimUIView
{
    [UIView beginAnimations:@"scrollUIViewAnim" context:nil];
    [UIView setAnimationDuration:20.0];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    CGPoint newContentOffset = CGPointMake(0, scrollView.contentSize.height);
    scrollView.contentOffset = newContentOffset;
    [UIView commitAnimations];
}

- (void)loadView {
    [super loadView];

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(5, 5, 200, 200)];
    scrollView.contentSize = CGSizeMake(200, 1000);
    scrollView.backgroundColor = [UIColor magentaColor];

    CGFloat y = 10;
    CGFloat yDelta = 21;
    for(int i = 0; i < 47; i++)
    {
     // Add some UILabels to the scrollview just so we can see it animating
     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, y, 180, 20)];
     label.text = [NSString stringWithFormat:@"Cheezburger %d", i];
     [scrollView addSubview:label];
     [label release];

     y += yDelta;
    }

    [self.view addSubview:scrollView];

    // Comment out one of these
    //[self startAnimTimer];
    [self startAnimUIView];
}
- (void)doScroll
{
        CGFloat scrollDelta = 1.0f;
    CGRect newRect = CGRectMake(0, currentRect.origin.y + scrollDelta, currentRect.size.width, currentRect.size.height);
    // Scroll the scrollview to the next position
    [scrollView scrollRectToVisible:newRect animated:YES];
    currentRect = newRect;
}
commanda
A: 

This method may help (or not):

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated

If not, just make a big UILabel and animate its frame using a CABasicAnimation.

Corey Floyd