首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当它在屏幕上水平滑动时循环的图像,看起来像它的无限滚动

当它在屏幕上水平滑动时循环的图像,看起来像它的无限滚动
EN

Stack Overflow用户
提问于 2013-01-21 10:29:45
回答 2查看 1.8K关注 0票数 5

我想有一个视图控制器,循环的图像在背景中,给人无限滚动的外观。例如,如果我使用下面的图像,左边缘和右边缘将完美匹配,并将用于提供无限滚动的外观。有没有人知道我会怎么设置这个?我做了一些研究,结果迷失了方向。谢谢大家!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-21 10:52:31

我会这样做的。编辑,而不是将其作为练习留给读者,更新以展示如何在任一方向上平移……

代码语言:javascript
复制
@property (nonatomic, assign) BOOL keepGoing;
@property (nonatomic, assign) BOOL panRight;
@property (nonatomic, strong) UIImageView *imageViewA;
@property (nonatomic, strong) UIImageView *imageViewB;

- (void)getReady {

    // init theImage
    UIImage *theImage = [UIImage imageNamed:@"theImage.png"];

    // get two copies of the image
    self.imageViewA = [[UIImageView alloc] initWithImage:theImage];
    self.imageViewB = [[UIImageView alloc] initWithImage:theImage];

    // place one in view, the other, off to the right (for right to left motion)
    NSInteger direction = (self.panRight)? 1 : -1;
    self.imageViewA.frame = self.view.bounds;
    self.imageViewB.frame = CGRectOffset(self.view.bounds, direction*self.imageViewA.bounds.size.width, 0.0);

    [self.view addSubview:self.imageViewA];
    [self.view addSubview:self.imageViewB];
    self.keepGoing = YES;
}

- (void)go {
    NSInteger direction = (self.panRight)? 1 : -1;
    CGFloat offsetX = -direction*self.imageViewA.bounds.size.width;

    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.imageViewA.frame = CGRectOffset(self.imageViewA.frame, offsetX, 0);
        self.imageViewB.frame = CGRectOffset(self.imageViewB.frame, offsetX, 0);
    } completion:^(BOOL finished) {
        if (self.keepGoing) {
            // now B is where A began, so swap them and reposition B
            UIImageView *temp = self.imageViewA;
            self.imageViewA  = self.imageViewB;
            self.imageViewB = temp;
            self.imageViewB.frame = CGRectOffset(self.view.bounds, direction*self.view.bounds.size.width, 0.0);
            // recursive call, but we don't want to wind up the stack
            [self performSelector:@selector(go) withObject:nil afterDelay:0.0];
        }
    }];
}

要使其生效,请设置panRight属性,然后调用[self getReady];[self go];

它将以异步方式运行。让它停止,set self.keepGoing = NO;

票数 8
EN

Stack Overflow用户

发布于 2013-01-21 10:52:59

一种简单的方法是使图像是屏幕宽度的两倍。将其设置为0,0。根据需要左右设置动画。一旦原点小于x轴上图像宽度的一半的负值,它就到达了右边缘的末端,一旦原点在x轴上大于0,它就到达了左边缘的末端。发生这种情况时,只需重置原点即可。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14431725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档