OGeek|极客世界-中国程序员成长平台

标题: ios - UIpagecontrol 需要从右到左的动画 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:21
标题: ios - UIpagecontrol 需要从右到左的动画

在我的应用程序中使用 UIControlpage。成功加载图像并在页面控件中显示。但我想显示图像动画,如从右到左移动。使用定时器调用页面控制并增加数组计数。

////使用 NSTimer 调用函数来显示动画。

[NSTimer scheduledTimerWithTimeInterval:5.0
                                     target:self
                                   selectorselector(loadNextController)
                                   userInfo:nil
                                    repeats:YES];

ScrollViewPage = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, frameX, frameY)];
ScrollViewPage.pagingEnabled = YES;
ScrollViewPage.backgroundColor=[UIColor redColor];
ScrollViewPage.delegate = self;
ScrollViewPage.backgroundColor = [UIColor clearColor];
ScrollViewPage.contentSize = CGSizeMake(frameX, frameY);
ScrollViewPage.showsHorizontalScrollIndicator = NO;


ScrollViewPage.pagingEnabled = YES;
pageControl = [[UIPageControl alloc] init];
pageControl.frame = CGRectMake(100,self.view.frame.size.height-100,self.view.frame.size.width-200,100);
pageControl.numberOfPages = [_pageImages count];
pageControl.currentPage = 0;

[self.view addSubview: ScrollViewPage];

for(int i = 0; i < [_pageImages count]; i++)
    {

        NSURL *url = [NSURL URLWithString:[_pageImages objectAtIndex:i]];
        NSData *data = [[NSData alloc] initWithContentsOfURL:url];
        UIImage *tmpImage = [UIImage imageWithData:data];
        _backgroundImageView = [[UIImageView alloc] initWithImage:tmpImage];
         _backgroundImageView.frame = CGRectMake(frameX * i, 0.0, frameX, frameY);
        [ScrollViewPage addSubview:_backgroundImageView];


    }

ScrollViewPage.contentSize = CGSizeMake(frameX*[_pageImages count], frameY);
pageControl.backgroundColor=[UIColor orangeColor];
[self.view addSubview:pageControl];
[pageControl addTarget:self actionselector(pageTurn forControlEvents:UIControlEventValueChanged];

/////这里调用定时器函数

- (void)loadNextController
{
     pageControl.currentPage = (pageControl.currentPage+1)%self.pageImages.count;
     ScrollViewPage.contentOffset = CGPointMake(pageControl.currentPage * self.view.bounds.size.width, 0);
}

这里我需要在 uipagecontrol 中显示从左到右的移动。帮帮我..



Best Answer-推荐答案


一般来说,您应该始终写下您当前的结果是什么,预期的结果是什么以及您尝试过什么。

我只能猜测您面临的问题是没有动画。 ScrollView 有一个 setContentOffset:animated 方法,您可以使用它来为过渡设置动画。另一种方法是使用 UIView animateWithDuration 并在方法 block 中设置偏移量。

尝试其中一个或两个...

[ScrollViewPage setContentOffset:CGPointMake(pageControl.currentPage * self.view.bounds.size.width, 0) animated:YES];

或者

[UIView animateWithDuration:0.2 animations:^{
        ScrollViewPage.contentOffset = CGPointMake(pageControl.currentPage * self.view.bounds.size.width, 0);
}];

编辑:循环图像。

要循环图像,您当时实际上只需要 2 个 ScrollView 上的图像。我将向您展示 3 的概念,您可以尝试使用它。

NSArray *allImages;
NSInteger currentIndex = 0;
UIScrollView *scrollView;
UIImageView *imageViews[3];

- (void)begin {

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*3.0, scrollView.frame.size.height); // always have 3 images at the time
    scrollView.contentOffset = CGPointMake(scrollView.frame.size.width, 0.0); // put to center

    for(int i=0; i<3; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(scrollView.frame.size.width*i, 0.0, scrollView.frame.size.width, scrollView.frame.size.height)];
        [scrollView addSubview:imageView];
        imageView[i] = imageView;
    }
    [self layoutForIndex:currentIndex];
}
- (void)nextImage {
    currentIndex++;
    [self layoutForIndex:currentIndex];

    scrollView.contentOffset = CGPointZero;
    [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0.0) animated:YES]
}

- (void)layoutForIndexNSInteger)index {
    if(allImages.count > 0) { // we need at least one image to do something
        for(int i=0; i<3; i++) {
            // do the circling
            NSInteger imageIndex = index-1+i;
            while(imageIndex < 0) imageIndex+=allImages.count;
            while(imageIndex >= allImages.count) imageIndex-=allImages.count;

            imageViews[i].image = imageViews[imageIndex];
        }
    }
}

但一般来说,您甚至不需要 ScrollView 。接下来,您只需在当前 View 旁边添加另一个 ImageView ,然后使用 UIView 动画重新定位两个 ImageView 。

关于ios - UIpagecontrol 需要从右到左的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36004271/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4