0

Im trying to make items spawn at the location of the cloud. So far I have the cloud animating on repeat just how i want it, but when I spawn an item the cloud animation resets from the beginning.

when adding a subview, What are my options?

Here is a gif from the project

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    gemCloudImage.center.x = 100
    makeCloudFly()
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)
}

func makeCloudFly() {
    UIView.animateWithDuration(6, delay: 1, options: [.Autoreverse, .Repeat], animations: {
        self.gemCloudImage.center.x = self.view.frame.width - 100
        }, completion: nil)
}

@IBAction func buttonTapped(sender: AnyObject) {

    let positionX: CGFloat = gemCloudImage.layer.presentationLayer()!.frame.midX
    let positionY: CGFloat = gemCloudImage.layer.presentationLayer()!.frame.midY
    print(positionX)
    print(positionY)

    let chair = UIImageView()
    chair.image = UIImage(named: "CHAIR1")
    chair.frame = CGRectMake(positionX, positionY, 50, 50)
    chair.frame = CGRect(x:positionX, y:positionY, width:50, height:50)
    self.view.addSubview(chair)

}
Jackster
  • 41
  • 7

1 Answers1

1

Move the code in viewDidLayoutSubviews to viewDidLoad. That should resolve your issue.

The method viewDidLayoutSubviews is called every time you add new subviews.

Felix
  • 35,354
  • 13
  • 96
  • 143
  • I placed the initial X position of the cloud into the animation function and moved it into viewDidLoad. Its now working pretty well ! – Jackster Jun 17 '16 at 15:20