Question: how to make a smooth scrolling background for the game, so that two pictures go one after the other constantly? In general, the fact is that there are about 5-6 pictures on the background and each layer should move at a different speed (the farther away the slower). Now what I did. I wrote a method that takes an array of pictures and does the following: I put some pictures on a visible screen, others (their copies) off the screen. Every picture that is visible on the screen at once, I put an animator in such (as on a piece of code) so that the animation repeats constantly, and pictures that are behind the screen I put in another animator (for some reason it doesn’t work in one), then I put both these animators In the array, and at the right time I run through the array and run the animation. The problem is that the animations start to behave completely wrong, get lost, those behind the screen move much faster than those on the screen (due to the fact that the pictures need to go a different distance at the same time) and generally look it is not very beautiful. Maybe someone knows how to arrange all this beautifully so that the layers move continuously and continuously, so that it is convenient to manage all of this.

PS Some images are not cut to fit, so you have to raise them a little by changing yPos.

func setUpBackGroundLayersWithArray(){ var xPos: CGFloat = 0 for (index,image) in self.backGroundsImages.reverse().enumerate(){ var yPos:CGFloat = -30 switch index { case 1: yPos = -10 xPos = 320 case 2: yPos = -10 default: yPos = -30 } let imageView = UIImageView(image: image) imageView.frame = CGRectMake(xPos, yPos, image.size.width, image.size.height) imageView.contentMode = .ScaleAspectFit self.addSubview(imageView) let copyimageView = UIImageView(image: image) copyimageView.frame = CGRectMake(320, yPos, image.size.width, image.size.height) copyimageView.contentMode = .ScaleAspectFit self.addSubview(copyimageView) self.layoutIfNeeded() let animator = UIViewPropertyAnimator(duration:8 - Double(index + 1), curve: .Linear, animations: { UIView.animateKeyframesWithDuration(0, delay: 0, options: [.Repeat], animations: { imageView.frame = CGRectMake(0 - copyimageView.frame.size.width, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height) }, completion: nil) }) let secondAnimator = UIViewPropertyAnimator(duration:10 - Double(index + 1), curve: .Linear, animations: { UIView.animateKeyframesWithDuration(0, delay: 0, options: [.Repeat], animations: { copyimageView.frame = CGRectMake(0-copyimageView.frame.size.width, copyimageView.frame.origin.y, copyimageView.frame.size.width, copyimageView.frame.size.height) }, completion: nil) }) self.animators.append(animator) self.animators.append(secondAnimator) } } 
  • show all code - Max Mikheyenko Nov.
  • @MaxMikheyenko interposed method screen. - Ildar.Z
  • No need to insert the code in the picture, the atoms will fly sneakers in you. Copy and format as normal. - VAndrJ

1 answer 1

In general, I have achieved the desired effect. If you suddenly need to explain in detail to someone - let me know. The final method for the background of pictures that will move at different speeds.

 func setUpBackGroundLayersWithArray(){ for (index,image) in self.backGroundsImages.reverse().enumerate(){ var yPos:CGFloat = -30 switch index { case 1: yPos = -10 case 2: yPos = -10 default: yPos = -30 } let imageView = UIImageView(image: image) imageView.frame = CGRectMake(0, yPos, image.size.width, image.size.height) imageView.contentMode = .ScaleAspectFit self.addSubview(imageView) let copyXPos = imageView.frame.size.width + imageView.frame.origin.x let copyimageView = UIImageView(image: image) copyimageView.frame = CGRectMake(copyXPos, yPos, image.size.width, image.size.height) copyimageView.contentMode = .ScaleAspectFit self.addSubview(copyimageView) self.layoutIfNeeded() let animator = UIViewPropertyAnimator(duration:startSpeed - Double(index), curve: .Linear, animations: { UIView.animateKeyframesWithDuration(0, delay: 0, options: [.Repeat,.CalculationModeDiscrete], animations: { imageView.frame = CGRectOffset(imageView.frame, -imageView.frame.size.width, 0) copyimageView.frame = CGRectOffset(copyimageView.frame, -copyimageView.frame.size.width, 0) }, completion:nil) }) self.animators.append(animator) } }