Animate infinite scrolling of an image in a seamless loop

前端 未结 3 1574
野趣味
野趣味 2020-12-02 13:43

Currently I have an image of clouds of 2048x435 that scrolls across a Landscaped oriented UIImageView of 1024x435 using CABasicAnimation. The cloud image scrolls as it shou

3条回答
  •  暖寄归人
    2020-12-02 14:24

    Rob, you rock brother. I was looking for a way to do the same thing but vertically. After reading your answer above I had no problem adjusting it to suit my needs. For anyone that may end up here on their hunt for a vertical solution this is what I ended up with, modeled after the above:

    - (IBAction)animateBackground6
    {
        UIImage *backgroundImage = [UIImage imageNamed:@"space.png"];
        UIColor *backgroundPattern = [UIColor colorWithPatternImage:backgroundImage];
    
        CALayer *background = [CALayer layer];
        background.backgroundColor = backgroundPattern.CGColor;
        background.transform = CATransform3DMakeScale(1, -1, 1);
        background.anchorPoint = CGPointMake(0, 1);
    
        CGSize viewSize = self.backgroundImageView.bounds.size;
        background.frame = CGRectMake(0, 0, viewSize.width,  backgroundImage.size.height +   viewSize.height);
        [self.backgroundImageView.layer addSublayer:background];
    
        CGPoint startPoint = CGPointZero;
        CGPoint endPoint = CGPointMake(0, -backgroundImage.size.height);
    
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        animation.fromValue = [NSValue valueWithCGPoint:endPoint];
        animation.toValue = [NSValue valueWithCGPoint:startPoint];
        animation.repeatCount = HUGE_VALF;
        animation.duration = 5.0;
        [background addAnimation:animation forKey:@"position"];
    }
    

提交回复
热议问题