I have a question. I want to display the user some content in a UIScrollView. I want to autoscroll the UIScrollView fast from bottom to top (like in the apple stores iPad). I tried to use DDAutoscrollview (If someone knows), but it doesn't work for me. Do have someone a solution for me to autoscroll a UIScrollView? Any code snippets would be nice.
.h
#interface Interface1 : UIViewController {
IBOutlet UIScrollView *scroller;
IBOutlet UILabel *warnung;
}
#property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
.m
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x,
self.scrollView.contentSize.height -
self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:NO];
CGPoint newOffset = self.scrollView.contentOffset;
newOffset.y = 0;
[self.scrollView setContentOffset:newOffset animated:YES];
}
- (void)viewDidLoad {
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 420)];
[super viewDidLoad];
}
Thanks.
> THUMBS UP FOR THE AWNSER THAT WAS GIVEN BY TOBI!!!
Just use setContentOffset:animated:
UIScrollView *scrollView = ...;
CGPoint newOffset = scrollView.contentOffset;
newOffset.y = 0;
[scrollView setContentOffset:newOffset animated:YES];
Edit:
To use it like some kind of start animation you could do this in the scrollView's view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
// ...
CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:NO];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGPoint newOffset = self.scrollView.contentOffset;
newOffset.y = 0;
[self.scrollView setContentOffset:newOffset animated:YES];
}
Edit 2 / 3:
To make the scrolling happen slower, use this:
- (void)viewDidLoad
{
[super viewDidLoad];
// ...
CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:NO];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
float scrollDuration = 4.0;
[UIView animateWithDuration:scrollDuration animations:^{
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
}];
}
Related
I am trying to update the UILabel of IBOutlet, however for some reason the content is not getting updated. Initially, the content is getting loaded from the model, which performs jsonResponse in async. However, I have set a delegate once the content is there to update the layout for which I am calling setNeedsDisplay.
Any help here would be appreciated.
Thanks.
The code below:
#interface pd ()
#property (nonatomic,retain) NSDictionary *pD;
#property (nonatomic, retain) pd_model *pdModel;
#end
#implementation pd
#synthesize pD = _pD, pdModel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil :(NSDictionary*)pD {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.productDetails = [[NSDictionary alloc] initWithDictionary:pD];
self.pdModel = [[ProductDetails_Model alloc] init:pD];
self.pdModel.delegate = self;
NSLog(#"%#",[self.pD description]);
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[scrollView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:scrollView];
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.scrollEnabled = YES;
// Do any additional setup after loading the view from its nib.
productRecCarousel = [[iCarousel alloc] initWithFrame:CGRectMake(0.0f, scrollView.contentSize.height, 320.0f, 100.0)];
productRecCarousel.delegate = self;
[scrollView addSubview:productRecCarousel];
[self addProductDescription];
NSLog(#"%#",[self.pdModel responseCode]);
[soldBy setText:[self.pdModel responseCode]];
//[soldBy setText:#"1"];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark
#pragma mark CarouselDelegate methods
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
return 1;
}
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel {
return 1;
}
- (UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
return [[UIView alloc] init];
}
- (CGFloat)carouselItemWidth:(iCarousel *)carousel {
//usually this should be slightly wider than the item views
return 150;
}
- (CATransform3D)carousel:(iCarousel *)_carousel transformForItemView:(UIView *)view withOffset:(CGFloat)offset {
//implement 'flip3D' style carousel
//set opacity based on distance from camera
view.alpha = 1.0 - fminf(fmaxf(offset, 0.0), 1.0);
//do 3d transform
CATransform3D transform = CATransform3DIdentity;
transform.m34 = _carousel.perspective;
transform = CATransform3DRotate(transform, M_PI / 8.0, 0, 1.0, 0);
return CATransform3DTranslate(transform, 0.0, 0.0, offset * _carousel.itemWidth);
}
- (void)addProductDescription {
NSLog(#"%f",addProductDetails.frame.size.height);
addProductDetails.frame = CGRectMake(0.0f, productRecCarousel.frame.size.height, addProductDetails.frame.size.width, addProductDetails.frame.size.height);
[scrollView addSubview:addProductDetails];
}
- (void)updateTheLayout {
//[self.view setNeedsDisplay];
//This is where I get the delegate called once the model is being updated. I tried scrollView and self.view setNeedsDisplay and setNeedsLayout but the IBoutlet never gets updated which is in "ViewDidLoad as assigned as" : [soldBy setText:[self.pdModel responseCode]];
}
Thanks.
so you just have to call [soldBy setText:[self.pdModel responseCode]]; from updateTheLayout and not from viewDidLoad because it is not ready yet. And please make sure to update the UI elements from the main thread.
You have to call [soldBy setText:[self.pdModel responseCode]]; in your callback, not in the viewDidLoad method.
I'm new to iPhone development and am working on this tutorial: http://www.iosdevnotes.com/2011/03/uiscrollview-paging/#comment-25166 - but am doing it in XCode 4.3.2. The example is pretty simple and I understand the code, but for some reason the scrollViewDidScroll function isn't firing for me in my ViewController.m (this thus doesn't change the Page Control pagination icons and update it to the current page). I placed an NSLog() in this function so I can tell if it is ever activating - but in my debug console I never see this text
The code from my ViewController.h is:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIScrollViewDelegate> {
UIScrollView* scrollView;
UIPageControl* pageControl;
}
#property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
#property (nonatomic, retain) IBOutlet UIPageControl* pageControl;
#end
The code from my ViewController.m is:
#import "ViewController.h"
#implementation ViewController
#synthesize scrollView, pageControl;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = colors.count;
}
//!!!!!!!!!!!!!!
//THIS IS WHERE MY PROBLEM IS -- THIS CODE DOESN'T SEEM TO EVER GET RUN EVEN WHEN I SCROLL!!!!!
//!!!!!!!!!!!!!
- (void)scrollViewDidScroll:(UIScrollView *)sender {
NSLog(#"this just fired");
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
self.scrollView = nil;
self.pageControl = nil;
}
- (void)dealloc {
[scrollView release];
[pageControl release];
[super dealloc];
}
#end
Any help would be greatly appreciated. I've been going back and forth from the downloadable file they have on the tutorial to my own over and over and over... I just feel like this thing should fire when I start scrolling and have no idea why it's not... Thanks in advance!
In Interface Builder you did not connect the scroll view’s “delegate” outlet to you view controller. But you can do it programmatically.
[self.scrollView setDelegate:self];
dianz is Right, Set your delegate.
[self.scrollView setDelegate:self];
OR
self.scrollView.delegate=self;
You forgot to set your delegate:
- (void)viewDidLoad {
[super viewDidLoad];
// This is what you forgot to do, set the delegate of the scrollView.
scrollView.delegate = self;
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
Please make sure you have set the scrollview delegate to the viewcontroller you want.
You can also attach the delegate in the xib file, if the scrollview is added in the xib.
Also, don't forget to implement the scrollview delegate protocol UIScrollViewDelegate in the viewcontroller's header file.
HI i'm tring to create photogallery with two scrollviews
one for horizontal scrolling between images and second for zooming images.
scrolling between images works fine but i'm not able to zoom image into second scrollView.
mainScrollView named VcLicences
VcLicences.h
#import <UIKit/UIKit.h>
#interface VcLicences : UIViewController <UIScrollViewDelegate>{
IBOutlet UIScrollView* scrollView;
IBOutlet UIPageControl* pageControl;
BOOL pageControlIsChangingPage;
}
#property (nonatomic, retain) UIView *scrollView;
#property (nonatomic, retain) UIPageControl* pageControl;
/* for pageControl */
- (IBAction)changePage:(id)sender;
/* internal */
- (void)setupPage;
#end
VcLicences.m
#import "VcLicences.h"
#import "VcImageZoomView.h"
#implementation VcLicences
#synthesize scrollView;
#synthesize pageControl;
- (void)setupPage
{
scrollView.delegate = self;
[self.scrollView setBackgroundColor:[UIColor whiteColor]];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.multipleTouchEnabled = NO;
NSUInteger nim = 0;
CGFloat cx = 0;
NSArray *licence = [NSArray arrayWithObjects:#"zl1.jpg", #"zl2.jpg", #"zl3.jpg", #"dph.jpg", #"or.jpg", nil];
for (nim=0; nim < [licence count]; nim++)
{
VcImageZoomView *imageZoom = [[VcImageZoomView alloc] initWithNibName:#"VcImageZoomView" bundle:nil];
[imageZoom.view setFrame:CGRectMake(cx, 0, 320, 332)];
[imageZoom setMyImage:[licence objectAtIndex:nim]];
[scrollView addSubview:imageZoom.view];
[imageZoom release];
cx += scrollView.frame.size.width;
}
self.pageControl.numberOfPages = nim;
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}
#pragma mark UIScrollViewDelegate stuff
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
if (pageControlIsChangingPage)
{
return;
}
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
pageControlIsChangingPage = NO;
}
- (IBAction)changePage:(id)sender
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * pageControl.currentPage;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
pageControlIsChangingPage = YES;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
self.navigationItem.title = #"CZECHMAT's licenses";
[self setupPage];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
//[scrollView release];
//[pageControl release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
return YES;
}
#end
second ScrollView for zooming images named VcImageZoomView
VcImageZoomView.h
#import <UIKit/UIKit.h>
#interface VcImageZoomView : UIViewController <UIScrollViewDelegate>{
IBOutlet UIScrollView *scroll;
UIImageView *image;
}
#property (nonatomic, retain) UIScrollView *scroll;
-(void)setMyImage:(NSString *) imageName;
#end
VcImageZoomView.m
#import "VcImageZoomView.h"
#implementation VcImageZoomView
#synthesize scroll;
-(void)setMyImage:(NSString *) imageName;
{
NSLog(#"%#", imageName);
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
float scaleWidth = scroll.frame.size.width / image.frame.size.width;
float scaleHeight = scroll.frame.size.height / image.frame.size.height;
float scale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
NSLog(#"%f", scale);
[scroll addSubview:image];
scroll.minimumZoomScale = scale;
scroll.maximumZoomScale = 1.0;
scroll.delegate = self;
[scroll setZoomScale:scroll.minimumZoomScale];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return image;
}
#end
thx for way
You really need to look at Apple's PhotoViewer sample and the WWDC'10 and '11 videos about ScrollViews: they are explaining in details (with this very example of a photoviewer scrolling and zooming) how to implement it:
How to recycle memory so that your memory footprint is drastically reduced (very important for fluidity)
How to handle both scrolling in one view and zooming in the other
If needed, how to do image tiling to optimize memory when zooming (only useful if you tiling is precomputed, so if you have images in your bundle anyway)
This is very interesting tutorials and videos and really worth reading/viewing.
https://github.com/doubleencore/KTPhotoBrowser
find above attached link get source code for photo gallery .....
I have this code, modified version of Apple's PageScrollView sample. Here the differnce is that m using ViewControllers instead of UIView.
MyClass.h
#interface MyClass : UIViewController {
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
BOOL pageControlUsed;
}
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
#property (nonatomic, retain) NSMutableArray *viewControllers;
- (IBAction)changePage:(id)sender;
#end
MyClass.m
#import "MyClass.h"
#import "MyViewController.h"
#import "MyViewControllerZero.h"
#import "MyViewControllerOne.h"
#import "MyViewControllerTwo.h"
static NSUInteger kNumberOfPages = 3;
#interface MyClass (PrivateMethods)
- (void)loadScrollViewWithPage:(int)page;
- (void)scrollViewDidScroll:(UIScrollView *)sender;
#end
#implementation MyClass
#synthesize scrollView, pageControl, viewControllers;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
- (void)loadScrollViewWithPage:(int)page {
// if (page < 0) return;
// if (page >= kNumberOfPages) return;
if(page==0)
{
MyViewControllerZero *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{ controller = [[MyViewControllerZero alloc] init];
[viewControllers replaceObjectAtIndex:page withObject:controller];
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
}
if(page==1)
{
MyViewControllerOne *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{ controller = [[MyViewControllerOne alloc] init];
[viewControllers replaceObjectAtIndex:page withObject:controller];
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
}
if(page==2)
{
MyViewControllerTwo *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{ controller = [[MyViewControllerTwo alloc] init];
[viewControllers replaceObjectAtIndex:page withObject:controller];
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (pageControlUsed) {
return;
}
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// A possible optimization would be to unload the views+controllers which are no longer visible
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlUsed = NO;
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlUsed = NO;
}
- (IBAction)changePage:(id)sender {
int page = pageControl.currentPage;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// update the scroll view to the appropriate page
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
pageControlUsed = YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[viewControllers release];
[scrollView release];
[pageControl release];
[super dealloc];
}
#end
The Above code works absolutely fine in the potrait mode. But when i change the orientation,the whole paging gets screwed up.. :((
please help me to resolve this problem..
Apple didn't design UIViewController to work at any point in a view hierarchy. The views that you're adding to your UIScrollView are managed by view controllers, but I think you'll find that the view controllers for those views don't receive rotation events.
You could try to manually forward all events from the scroll view's controller to the child view controllers, but I think you'll find that to be tedious and error prone. I'd suggest instead that you simply use a single view controller for the scroll view and its child views.
I just want in my application a ticker,
i have no idea to implement ticker please tell me.
Thanks
Everything you need to do this is in the SDK, no need to customize at all. I haven't checked this, but you can try the following:
#import <UIKit/UIKit.h>
#interface TickerScrollView : UIScrollView {
UILabel *textLabel;
}
- (void)displayText:(NSString *)string;
- (void)clearTicker;
#property (nonatomic, retain, readwrite) UILabel *textLabel;
#end
////
#import "TickerScrollView.h"
#interface TickerScrollView()
- (void)initialiseTextLabel;
- (void)clearTicker;
- (void)beginAnimation;
#end
#implementation TickerScrollView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
[self setFrame: frame];
[self setBounces: NO];
[self setUserInteractionEnabled:NO];
[self setShowsVerticalScrollIndicator:NO];
[self setShowsHorizontalScrollIndicator:NO];
[self initialiseTextLabel];
}
return self;
}
- (void)initialiseTextLabel {
textLabel = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];
[textLabel setTextAlignment:UITextAlignmentLeft];
[textLabel setNumberOfLines:1];
[textLabel sizeToFit];
[self addSubview:textLabel];
[self sendSubviewToBack:textLabel];
[self setScrollEnabled:YES];
}
- (void)displayText:(NSString *)string {
[self clearTicker];
[textLabel setText:string];
[textLabel sizeToFit];
[self setContentSize:textLabel.frame.size];
[self beginAnimation];
}
- (void)clearTicker {
[textLabel setText:#""];
[textLabel sizeToFit];
CGPoint origin = CGPointMake(0, 0);
[self setContentOffset:origin];
}
- (void)beginAnimation {
CGFloat text_width = textLabel.frame.size.width;
CGFloat display_width = self.frame.size.width;
if ( text_width > display_width ) {
CGPoint origin = CGPointMake(0, 0);
[self setContentOffset:origin];
CGPoint terminal_origin = CGPointMake(textLabel.frame.size.width - self.frame.size.width, textLabel.frame.origin.y);
float duration = (text_width - display_width)/50;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDelay:1.0];
[UIView setAnimationDuration:duration];
[self setContentOffset:terminal_origin];
[UIView commitAnimations];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void)dealloc {
[textLabel release];
[super dealloc];
}
#synthesize textLabel;
#end
Assuming by "Ticker" you mean a horizontally scrolling text:
A ticker is basically just a text string that is moving by having its x coordinate changed continuously. Check this simple tutorial on how to display a label:
http://knol.google.com/k/iphone-sdk-helloworld
Then later you can animate it by using an NSTimer to call a method updating the labels x coordinate continuously.