Need help with scrolling marquee
Need help with scrolling marquee
- Subject: Need help with scrolling marquee
- From: "Michael A. Crawford" <email@hidden>
- Date: Thu, 11 Mar 2010 13:23:43 -0500
I'm trying to create an effect on the iPhone to scroll text across the view in a given location. Not really sure how to go about it so I did some experimenting. Af first I tried to animate the scroll using a timer. That gave me inconsistent movement; the velocity of the scrolling text appeared to be non-constant. Then I remembered that CoreAnimation is really supposed to take care of this kind of animation for you so I switch to trying to get it to manage the timing for me. I must be doing it all wrong because I cannot control the velocity of the scrolling text.
I expect my whole approach is probably wrong and am hoping that one of you has a better approach or suggestions as to how to fix it. I haven't found much in the way of CAScrollView documentation or examples to help. I've included the code below. Hopefully the length will not be an issue.
//
// ScrollingMarqueeViewController.m
// ScrollingMarquee
//
// Created by Michael A. Crawford on 3/11/10.
// Copyright Crawford Design Engineering, LLC 2010. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "ScrollingMarqueeViewController.h"
@implementation ScrollingMarqueeViewController
@synthesize scrollLabel;
- (CALayer*)textLayer
{
scrollLabel.text = @"this is a long line of text that should be too long for "
@"the screen width of this label.";
return scrollLabel.layer;
}
- (CAScrollLayer*)scrollLayer
{
CAScrollLayer* layer = [CAScrollLayer layer];
[layer addSublayer:[self textLayer]];
return layer;
}
- (void)timerFireMethod:(NSTimer*)theTimer
{
#if 0
// Here I was using a periodic timer to animate the scroll. I noticed that
// the animation wasn't smooth and then remembered that CA is supposed to do
// the animating for me. So, I switched to trying the code below but that
// doesn't work either. I'm really just grasping at straws here.
static CGPoint origin = {0.0f, 0.0f};
origin.x += 5.0f;
[scrollLayer scrollToPoint:origin];
#else
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationDuration:5.0];
[scrollLayer scrollToPoint:CGPointMake(320.0f, 0.0f)];
[UIView commitAnimations];
#endif
}
- (void)viewDidLoad
{
[super viewDidLoad];
// create the scroll layer and add it to our view
scrollLayer = [self scrollLayer];
[self.view.layer addSublayer:scrollLayer];
// make it the same size as our label
scrollLayer.bounds = scrollLabel.frame;
// position it in the middle of the view
scrollLayer.position = CGPointMake(320.0f * 0.5f, 480.0f * 0.5f);
// scroll the text horizontally
scrollLayer.scrollMode = kCAScrollHorizontally;
// use a timer to make the scroll happen
scrollTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timerFireMethod:)
userInfo:nil
repeats:/*YES*/NO];
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden