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

时间:2013-01-21 02:29:45

标签: ios objective-c uiimageview uiimage

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

Infinite Scrolling Image

2 个答案:

答案 0 :(得分: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];[self go];

它主要运行asynch。要停止,set self.keepGoing = NO;

答案 1 :(得分:0)

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