5

我想要一个视图控制器,它在背景中循环图像以呈现无限滚动的外观。例如,如果我使用下面的图像,左右边缘完美匹配,将用于提供无限滚动的外观。有谁知道我可能会如何进行设置?我做了一些研究,迷路了。谢谢你们!

无限滚动图片

4

2 回答 2

8

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

@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];and [self go];

它将主要异步运行。为了让它停下来,set self.keepGoing = NO;.

于 2013-01-21T02:52:31.167 回答
0

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

于 2013-01-21T02:52:58.893 回答