How to add auto scroll functionality to a view in iphone sdk?

删除回忆录丶 提交于 2019-12-25 06:24:43

问题


I'm new to auto scroll functionality. I have a very lengthy .png file. what I want to do is, when I click a button I want to automatically start scrolling of that image. I had gone through scrollView sample code in sdk. but I get confused. Please any one help me

Thanks in advance Praveena


回答1:


Scrolling is achieved by setting the content offset.

Imagine a view controller constructed something like this (e.g. in -viewDidLoad):

// Load image.
UIImage *image = [UIImage imageNamed:@"image.png"];

// Create image view to hold the image.
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, [image size].width, [image size].height)];

[imageView setImage:image];

// Create a scroll view that is the size of the view.
scrollView = [[UIScrollView alloc] initWithFrame:[[self view] bounds]];

// Important: If the content size is less than the scroll view frame, then it will not scroll.
[scrollView setContentSize:[image size]];

// Add to view hierarchy.
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];

To make it scroll, just do this:

[scrollView setContentOffset:CGPointMake(0, 100) animated:YES];

To have the scrolling to be continuous, you need to set up a timer that updates the content offset.




回答2:


- (void) scrollView
{
    CGFloat currentOffset = scrollImage.contentOffset.x;

    CGFloat newOffset;

    if(currentOffset == 3840)
    {
        currentOffset = -320.0;

        [scrollImage setContentOffset:CGPointMake(newOffset,0.0) animated:NO];
    }

    newOffset = currentOffset + 320;

        [UIScrollView beginAnimations:nil context:NULL]; 
        [UIScrollView setAnimationDuration:2.1]; 
        [scrollImage setContentOffset:CGPointMake(newOffset,0.0)];
        [UIScrollView commitAnimations];
}


来源:https://stackoverflow.com/questions/6354360/how-to-add-auto-scroll-functionality-to-a-view-in-iphone-sdk

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