2017-11-15 8 views
0

Mit dem folgenden Code, ich bin ein CALayer animieren können von links nach rechtsCABasicAnimation: Von links nach rechts

let imageLayer = CALayer() 
imageLayer.frame = CGRect(x: 0, y: 500, width: 100, height: 70) 
imageLayer.contents = UIImage(named: "first_image")?.cgImage 

let animation = CABasicAnimation() 
animation.keyPath = "position.x" 
animation.fromValue = 100 
animation.toValue = 300 
animation.duration = 3 
animation.fillMode = kCAFillModeForwards 
animation.isRemovedOnCompletion = true 
animation.repeatCount = 2 
imageLayer.add(animation, forKey: "basic") 

self.view.layer.addSublayer(imageLayer) 

Was ich versuche zu tun, in

Zyklus : 1 - "first_image" wird von links nach rechts

und in

bewegen

Zyklus: 2 - "second_image" (dies ist ein anderes Bild) bewegt sich von links nach rechts wie vorheriger Zyklus.

Hier Zyklus: 1 + Zyklus: 2 ist eigentlich eine einzelne Animation und ich möchte diese Animation für eine Reihe von Zeiten geschehen. Wie erreiche ich das? Jede Hilfe wäre willkommen.

Antwort

0

Wie Sie von links nach rechts erforderlich & umgekehrt Animation, können Sie auch UIView.anima.. für die Animation verwenden.

 let testViewTowardsLeft = UIView(frame: CGRect(x: 0, y: 150, width: 50, height: 50)) 
    testViewTowardsLeft.backgroundColor = .red 
    view.addSubview(testViewTowardsLeft) 
    UIView.animate(withDuration: 1, delay: 0, options: .repeat, animations: {Bool in 
     testViewTowardsLeft.frame = CGRect(x: 100, y: 150, width: 50, height: 50) 

    }, completion: nil) 


    let testViewTowardsRight = UIView(frame: CGRect(x: 100, y: 100, width: 50, height: 50)) 
    testViewTowardsRight.backgroundColor = .red 
    view.addSubview(testViewTowardsRight) 
    UIView.animate(withDuration: 1, delay: 0, options: .repeat, animations: {Bool in 
     testViewTowardsRight.frame = CGRect(x: 0, y: 100, width: 50, height: 50) 

    }, completion: nil) 

enter image description here

Verwandte Themen