2016-09-22 84 views
0

我有這段代碼,使圖像移動,但我希望這在打開頁面5秒後顯示。顯示和移動圖像

我該怎麼做?由於

@IBOutlet weak var moveobj: UIImageView! 



override func viewDidAppear(animated: Bool) { 


    super.viewDidAppear(animated) 


    let orbit = CAKeyframeAnimation(keyPath: "position") 
    var affineTransform = CGAffineTransformMakeRotation(0.0) 
    affineTransform = CGAffineTransformRotate(affineTransform, CGFloat(M_PI)) 
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: 198 - (100/2),y: 135 - (100/2)), radius: CGFloat(155), startAngle: CGFloat(255), endAngle:CGFloat(M_PI * 2), clockwise: true) 


    orbit.path = circlePath.CGPath 
    orbit.duration = 18 
    orbit.additive = true 
    orbit.repeatCount = 0.15 
    orbit.calculationMode = kCAAnimationPaced 


    moveobj.layer .addAnimation(orbit, forKey: "orbit") 
+0

所以,你要5秒顯示圖像? –

+0

不,我想顯示這個圖像5秒後,我打開一個頁面,並開始移動 – dinis

+0

例如我打開一個頁面等待5秒,這開始移動 – dinis

回答

0

要等待5秒鐘,你做任何事情之前,你可以使用一個Timer,見下文如何使用它:

override func viewDidLoad() { 
    super.viewDidLoad() 

    // start a new timer with an interval of 5 seconds 
    Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(ViewController.showImage), userInfo: nil, repeats: false) 
} 

// after 5 seconds this class will be called and here you can do your work 
func showImage(){ 
    print("Showing") 
} 
+0

我把這個,但這不能做任何事 – dinis

+0

什麼都沒有工作? –

+0

已經工作thkx – dinis

1

我不知道這是否你在找什麼。除了使用像Rashwan建議的計時器之外,您可以使用GCD

viewDidAppear(),顯示圖片,然後添加以下代碼,延時5秒,事情是這樣的:

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

    loadImage() 

    let delayInSeconds = 5.0 
    let popTime = dispatch_time(DISPATCH_TIME_NOW, 
     Int64(delayInSeconds * Double(NSEC_PER_SEC))) 
    dispatch_after(popTime, dispatch_get_main_queue()) { 
    //You can add some "finish up" operations here 
    } 
} 
+0

我把這個,但這不能做任何事 – dinis

+0

你把它放在哪裏?以前執行什麼?後? – Idan

+0

是的,它在5秒內沒有做任何事情。 – Idan