循环UIScrollView [英] Loop an UIScrollView

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

问题描述


可能重复:

UIScrollView。关于实现无限的任何想法滚动/缩放?


我有一个UIScrollView, (约30)。我想让它成为可能,当用户到达最后一个图像显示第一个后,以此类推。我想实现与第一个图像相同的功能(去到最后一个)。
我想循环图像顺利,用户甚至不会注意到他正在做另一个循环。



什么是最好的方式来实现?



谢谢。

解决方案

我有一个图像数组添加到scrollview所以我首先添加最后一个到scrollview,然后运行通过数组添加所有的图像,然后,再次从数组中添加第一个。 p>

看看这段代码:

  #importMainViewController.h 
#importMainView.h

#define WIDTH_OF_SCROLL_PAGE 320
#define HEIGHT_OF_SCROLL_PAGE 352
#define WIDTH_OF_IMAGE 320
#define HEIGHT_OF_IMAGE 352
#define LEFT_EDGE_OFSET 0

@implementation MainViewController

@synthesize scrollView,slideImages;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]){
//自定义初始化
}
return self;
}

- (void)viewDidLoad {
scrollView = [[UIScrollView alloc] init];
CGRect scrollFrame;
scrollFrame.origin.x = 0;
scrollFrame.origin.y = 0;
scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE;
scrollFrame.size.height = HEIGHT_OF_SCROLL_PAGE;

scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.bounces = YES;
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
scrollView.userInteractionEnabled = YES;

slideImages = [[NSMutableArray alloc] init];
[slideImages addObject:@welcome-small.jpg];
[slideImages addObject:@football-small.jpg];
[slideImages addObject:@dancing-small.jpg];
[slideImages addObject:@celebration-small.jpg];

//添加最后一个图像
UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count] -1)]]];
imageView.frame = CGRectMake(LEFT_EDGE_OFSET,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[imageView release];

for(int i = 0; i <[slideImages count]; i ++){
//循环此位
UIImageView * imageView = [[UIImageView alloc] initWithImage: UIImage imageNamed:[slideImages objectAtIndex:i]]];
imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i)+ LEFT_EDGE_OFSET + 320,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[imageView release];
//
}

//在最后添加第一个图像
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0 ]]];
imageView.frame = CGRectMake((WIDTH_OF_IMAGE *([slideImages count] +1))+ LEFT_EDGE_OFSET,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[imageView release];

[scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE *([slideImages count] + 2),HEIGHT_OF_IMAGE)];
[scrollView setContentOffset:CGPointMake(0,0)];
[self.view addSubview:scrollView];
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE)animated:NO];
[super viewDidLoad];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int currentPage = floor((self.scrollView.contentOffset.x - self.scrollView.frame.size .width /([slideImages count] +2))/ self.scrollView.frame.size.width)+ 1;
if(currentPage == 0){
// go last but 1 page
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE * [slideImages count],0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE)animated:没有];
} else if(currentPage ==([slideImages count] +1)){
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE)animated:NO];
}
}


- (void)didReceiveMemoryWarning {
//如果没有超级视图,则释放视图。
[super didReceiveMemoryWarning];

//释放所有未使用的缓存数据,图像等。
}

- (void)viewDidUnload {
//释放主视图的任何保留的子视图。
//例如self.myOutlet = nil;
}


- (void)dealloc {
[scrollView release];
[slideImages发布]
[super dealloc]
}


@end

使用scrollViewDidEndDecelerating检查用户在滚动的位置,然后跳转到第二个图像或最后一个图像,所以他们可以连续滚动...对不起,如果我没有解释好 - 时间不足!但这个代码在我的设备上工作正常..


Possible Duplicate:
UIScrollView. Any thoughts on implementing “infinite” scroll/zoom?

I've got an UIScrollView and in it different images(about 30). I'd like to make it possible, when user reaches the last image to show the first one after it and so on. And I want to implement the same feature with the first image(to go to the last one). I'd like to loop the images smoothly that user won't even notice that he is making another loop.

What is the best way to achieve this?

Thanks.

解决方案

I've just done this. I have an array of images to add to the scrollview so I first of all add the last one to the scrollview, then run through the array to add all the images and then after that, add the first one from the array again.

Have a look through this code:

#import "MainViewController.h"
#import "MainView.h"

#define WIDTH_OF_SCROLL_PAGE 320
#define HEIGHT_OF_SCROLL_PAGE 352
#define WIDTH_OF_IMAGE 320
#define HEIGHT_OF_IMAGE 352
#define LEFT_EDGE_OFSET 0

@implementation MainViewController

@synthesize scrollView, slideImages;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    scrollView = [[UIScrollView alloc] init];
    CGRect scrollFrame;
    scrollFrame.origin.x = 0;
    scrollFrame.origin.y = 0;  
    scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE;
    scrollFrame.size.height = HEIGHT_OF_SCROLL_PAGE;

    scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
    scrollView.bounces = YES;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    scrollView.userInteractionEnabled = YES;

    slideImages = [[NSMutableArray alloc] init];
    [slideImages addObject:@"welcome-small.jpg"];
    [slideImages addObject:@"football-small.jpg"];
    [slideImages addObject:@"dancing-small.jpg"];
    [slideImages addObject:@"celebration-small.jpg"];

    //add the last image first
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count]-1)]]];
    imageView.frame = CGRectMake(LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
    [scrollView addSubview:imageView];
    [imageView release];

    for (int i = 0;i<[slideImages count];i++) {
        //loop this bit
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];
        imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + 320, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
        [scrollView addSubview:imageView];
        [imageView release];
        //
    }

    //add the first image at the end
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0]]];
    imageView.frame = CGRectMake((WIDTH_OF_IMAGE * ([slideImages count] + 1)) + LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
    [scrollView addSubview:imageView];
    [imageView release];

    [scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([slideImages count] + 2), HEIGHT_OF_IMAGE)];
    [scrollView setContentOffset:CGPointMake(0, 0)];
    [self.view addSubview:scrollView];
    [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO]; 
    [super viewDidLoad];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int currentPage = floor((self.scrollView.contentOffset.x - self.scrollView.frame.size.width / ([slideImages count]+2)) / self.scrollView.frame.size.width) + 1;
    if (currentPage==0) {
        //go last but 1 page
        [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE * [slideImages count],0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
    } else if (currentPage==([slideImages count]+1)) {
        [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
    }
}


- (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 {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [scrollView release];
    [slideImages release];
    [super dealloc];
}


@end

I then use scrollViewDidEndDecelerating to check where in the scroll the user is, and then jump them to the 2nd image or last but 1 image so they can scroll continuously... Sorry if I haven't explained it well - short of time! But this code is working fine for me on the device..

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

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