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

Bra*_*don 5 objective-c uiimageview uiimage ios

我想有一个视图控制器在背景中循环图像,使外观无限滚动.例如,如果我使用下面的图像,左右边缘完美匹配,并将用于提供无限滚动外观.有谁知道我怎么设置这个?我做了一些研究而且迷路了.谢谢你们!

无限滚动图像

dan*_*anh 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];
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)

要进行操作,请设置panRight属性,然后调用[self getReady];[self go];.

它将主要运行asynch.为了让它停止,set self.keepGoing = NO;.