无限scrollView动画 [英] animation with infinite scrollView

查看:124
本文介绍了无限scrollView动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在无限滚动视图中有5张图片。

I have 5 pictures inside an infinite scrollView.

所以为了使滚动无限/循环我这样定位我的图像:

So in order to make that scrollView infinite/circular I positioned my images like this:

5 1 2 3 4 5 1
含义:上一张图片第一张图片第二张图片.....上一张图片第一张图片

5 1 2 3 4 5 1 meaning: last picture first picture second picture.....last picture first picture

为了让它变得无限,我有以下代码:

And in order for it to become infinite I have the following code:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if(self.scrollView.contentOffset.x == 0){

    [self.scrollView scrollRectToVisible:CGRectMake(self.view.frame.size.width*pathImages.count, 0, self.view.frame.size.width, self.view.frame.size.height) animated:NO];  
        }
    else if (self.scrollView.contentOffset.x == self.view.frame.size.width*(pathImages.count+1)) {         

    [self.scrollView scrollRectToVisible:CGRectMake(self.view.frame.size.width,0 ,self.view.frame.size.width, self.view.frame.size.height) animated:NO];    
    }
}

这意味着当我在最后一张照片时 scrollView contentOffset 设置为转到第一张图片 - 这样我获得了无限 scrollView。

which means that when I'm at the last picture the contentOffset of the scrollView is set to go to the first picture-in this way I get an infinite scrollView.

  What I wanted next was for my scrollView to slide automatically-for this I set a timer which calls one method-onTimer:

- (void) onTimer{
    NSLog(@"flip pages");
    if(h < pathImages.count*self.view.frame.size.width)
    {
        h+= self.view.frame.size.width;

    }
    else
    {
        h=self.view.frame.size.width;
    }

    if(self.scrollView.contentOffset.x == 0){

        [self.scrollView scrollRectToVisible:CGRectMake(self.view.frame.size.width*pathImages.count, 0, self.view.frame.size.width, self.view.frame.size.height) animated:NO];  

    }

    if (self.scrollView.contentOffset.x == self.view.frame.size.width*pathImages.count)         

    {      

        [self.scrollView scrollRectToVisible:CGRectMake(self.view.frame.size.width,0 ,self.view.frame.size.width, self.view.frame.size.height) animated:NO];                         

    }

    else

    [UIView animateWithDuration:1 
                          delay:0 
                        options:UIViewAnimationOptionCurveEaseInOut 
                     animations:^{ self.scrollView.contentOffset = CGPointMake(h, 0); } 
                     completion:NULL];
}

这条神奇的线条:

[UIView animateWithDuration:1 
                              delay:0 
                            options:UIViewAnimationOptionCurveEaseInOut 
                         animations:^{ self.scrollView.contentOffset = CGPointMake(h, 0); } 
                         completion:NULL];

通过动画自动执行scoll。

does the scoll automatically with animation.

除此之外一切都很棒:

在我查看最后一张图片后,我应该设置滚动视图的偏移量,以便回到带有动画的第一张图片。

after I view the last picture I should set the offset of scroll view in order to get back to the first picture with animation.

好吧,如果我这样做:

if (self.scrollView.contentOffset.x == self.view.frame.size.width*pathImages.count)         
            {      
        [UIView animateWithDuration:1 
                                  delay:0 
                                options:UIViewAnimationOptionCurveEaseInOut 
                             animations:^{     [self.scrollView scrollRectToVisible:CGRectMake(self.view.frame.size.width,0 ,self.view.frame.size.width, self.view.frame.size.height) animated:NO];  } 
                             completion:NULL];                        

        }

查看完最后一张图片后才能到达第一张图片......它遍历所有其他图片。

after the last picture is viewed in order to get to the first picture...it loops through all the other pictures.

我想要的是这样的:在我查看最后一张图片后让我回到第一张图片应该是使用动画加载到屏幕上,但没有查看它们之间的所有其他图片。谢谢

What I want is this:after I view the last picture to get me back to the first picture which should be loaded on screen using animation, but without viewing all the other pictures between them.Thanks

推荐答案

如果我看到这个正确,问题是,您再次使用动画将视图滚动回零位置。我相信你需要修改你发布的最后一段代码:

If I see this correctly, the problem is, that you again use the animation to scroll the view back to zero position. I believe you need to modify the last bit of code you posted to something like this:

if (self.scrollView.contentOffset.x == self.view.frame.size.width*pathImages.count)         
            {      
        [self.scrollView scrollRectToVisible:CGRectMake(self.view.frame.size.width,0 ,self.view.frame.size.width, self.view.frame.size.height) animated:NO];//no animation on returning
[self onTimer];//even if this code is already inside method "onTimer"
        }

而不是你在做什么尝试使用只显示3到5张图像的滚动视图当时(如果他们是全屏)。如果您的应用程序中有许多图像,它会因为高内存消耗而崩溃。尝试玩这个几乎你想要的测试例子:

Rather then what you are doing try using a scrollview that displays only 3 to 5 images at the time (if they are fullscreen). If you will have many images in your application, it will crash because of high memory consumption. Try playing with this test example that does nearly what you want:

HEADER:

#import <Foundation/Foundation.h>

@interface IScrollView : UIScrollView <UIScrollViewDelegate> {
    NSMutableArray *imagePaths;
    UIImageView *imageViews[3];
    NSInteger currentImage;
    NSTimer *animationTimer;//weak link
}
@end

SOURCE:

#import "IScrollView.h"

@implementation IScrollView
- (UIImage *)imageFromResourcesWithName:(NSString *)name {
    UIImage *ret = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:name]];
    return [ret autorelease];
}
- (id)initWithFrame:(CGRect)frame {
    if((self = [super initWithFrame:frame])) {
        imagePaths = [[NSMutableArray alloc] init];
        [imagePaths addObject:@"imag1.png"];
        [imagePaths addObject:@"imag2.png"];
        [imagePaths addObject:@"imag3.png"];
        [imagePaths addObject:@"imag4.png"];
        [imagePaths addObject:@"imag5.png"];
        imageViews[0] = [[UIImageView alloc] initWithFrame:CGRectMake(320.0f*0, .0f, 320.0f, 480.0f)];
        imageViews[1] = [[UIImageView alloc] initWithFrame:CGRectMake(320.0f*1, .0f, 320.0f, 480.0f)];
        imageViews[2] = [[UIImageView alloc] initWithFrame:CGRectMake(320.0f*2, .0f, 320.0f, 480.0f)];
        [self addSubview:imageViews[0]];
        [self addSubview:imageViews[1]];
        [self addSubview:imageViews[2]];
        imageViews[0].image = [self imageFromResourcesWithName:[imagePaths objectAtIndex:0]];
        imageViews[1].image = [self imageFromResourcesWithName:[imagePaths objectAtIndex:1]];
        imageViews[2].image = [self imageFromResourcesWithName:[imagePaths objectAtIndex:2]];
        currentImage = 1;
        self.contentOffset = CGPointMake(320.0f*currentImage, .0f);
        self.contentSize = CGSizeMake(3.0f*320.0f, 480.0f);
        self.delegate = self;
        animationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(scrollFragment) userInfo:nil repeats:YES];
    }
    return self;
}
- (void)repositionIfNeeded {
    CGFloat offsetX = self.contentOffset.x;
    NSInteger iCount = [imagePaths count];
    if(offsetX > 320.0f*1.75f) {
        self.contentOffset = CGPointMake(offsetX-320.0f, .0f);
        imageViews[0].image = imageViews[1].image;
        imageViews[1].image = imageViews[2].image;
        NSInteger imageToLoad = currentImage+2;
        if(imageToLoad>iCount-1)
            imageToLoad -= iCount;
        imageViews[2].image = [self imageFromResourcesWithName:[imagePaths objectAtIndex:imageToLoad]];
        currentImage++;
        if(currentImage>iCount-1)
            currentImage -= iCount;
    }
    else if(offsetX < 320.0f*.25f) {
        self.contentOffset = CGPointMake(offsetX+320.0f, .0f);
        imageViews[2].image = imageViews[1].image;
        imageViews[1].image = imageViews[0].image;
        NSInteger imageToLoad = currentImage-2;
        if(imageToLoad<0)
            imageToLoad += iCount;
        imageViews[0].image = [self imageFromResourcesWithName:[imagePaths objectAtIndex:imageToLoad]];
        currentImage--;
        if(currentImage<0)
            currentImage += iCount;
    }
}
- (void)scrollFragment {
    self.contentOffset = CGPointMake(self.contentOffset.x+1.0, .0f);
    [self repositionIfNeeded];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self repositionIfNeeded];
}
- (void)dealloc {
    [imageViews[0] release];
    [imageViews[1] release];
    [imageViews[2] release];
    [imagePaths release];
}
@end

这篇关于无限scrollView动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆