【问题标题】:Scrolling an image continuosly连续滚动图像
【发布时间】:2012-02-16 06:44:57
【问题描述】:

我需要在我的项目中连续滚动图像,即从图像的底部开始滚动一次通过继续滚动到顶部应该从底部开始等等,在一种循环中,我怎么能这样做呢?

谢谢

【问题讨论】:

标签: iphone uiscrollview uiimageview scroll uiimage


【解决方案1】:

在这个例子中,我总共拍摄了 5 张图片

- (void)viewDidLoad {
        [super viewDidLoad];

        // add the last image (image4) into the first position
        [self addImageWithName:@"image4.jpg" atPosition:0];

        // add all of the images to the scroll view
        for (int i = 1; i < 5; i++) {
            [self addImageWithName:[NSString stringWithFormat:@"image%i.jpg",i] atPosition:i];
        }

        // add the first image (image1) into the last position
        [self addImageWithName:@"image1.jpg" atPosition:5];

        scrollView.contentSize = CGSizeMake(320, 2496);    
        [scrollView scrollRectToVisible:CGRectMake(0,416,320,416) animated:NO]; 
    }

    - (void)addImageWithName:(NSString*)imageString atPosition:(int)position {
        // add image to scroll view
        UIImage *image = [UIImage imageNamed:imageString];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = CGRectMake(0,position*416,320, 416);
        [scrollView addSubview:imageView];
        [imageView release];
    }

实现委托方法

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {    
    NSLog(@"%f",scrollView.contentOffset.y);
    // The key is repositioning without animation      
    if (scrollView.contentOffset.y == 0) {         
        // user is scrolling to the left from image 1 to image 4         
        // reposition offset to show image 4 that is on the right in the scroll view         
        [scrollView scrollRectToVisible:CGRectMake(0,1664,320,416) animated:NO];     
    }    
    else if (scrollView.contentOffset.y == 2080) {         
        // user is scrolling to the right from image 4 to image 1        
        // reposition offset to show image 1 that is on the left in the scroll view         
        [scrollView scrollRectToVisible:CGRectMake(0,416,320,416) animated:NO];         
    } 
}

【讨论】:

    【解决方案2】:

    我为自动滚动滚动视图提供了两个参考链接,您还可以在其中获取源代码并将其实现到您的项目中,这是惊人的功能

    1) Automatically scroll the scroll view

    2) As par your requirement created post of like wise gallery.

    希望这篇文章对你有用。

    愉快的编码 感谢和问候,

    @塞缪尔

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      • 1970-01-01
      • 2015-08-14
      • 2017-12-06
      • 1970-01-01
      相关资源
      最近更新 更多