gpt4 book ai didi

iphone - 无限 ScrollView 的动画

转载 作者:行者123 更新时间:2023-12-03 20:45:18 26 4
gpt4 key购买 nike

我在无限 ScrollView 中有 5 张图片。

因此,为了使 scrollView 无限/循环,我将图像定位如下:

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

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

- (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];
}
}

这意味着当我在最后一张图片时,scrollViewcontentOffset 设置为转到第一张图片 - 这样我就得到了无限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];

自动滚动动画。

一切都很棒,除了这个:

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

如果我这样做:

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];

}

在查看最后一张图片以到达第一张图片之后...它会循环浏览所有其他图片。

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

最佳答案

如果我正确地看到了这一点,问题是,您再次使用动画将 View 滚动回零位置。我相信您需要修改您发布的最后一段代码,如下所示:

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 个图像的 ScrollView (如果它们是全屏的)。如果您的应用程序中有很多图像,它会因为高内存消耗而崩溃。尝试使用这个测试示例,它几乎可以满足您的要求:

标题:

#import <Foundation/Foundation.h>

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

来源:

#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

关于iphone - 无限 ScrollView 的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8357018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com