2014-06-08 14 views
5

In rapido, sto passando un oggetto alla vista e ne ho bisogno per farlo scorrere o sfumare. Come posso ottenere questo tipo di animazione nel mio programma?Anima oggetti in Swift?

ho tutti i miei elementi in vista a livello di codice senza IB (Interface Builder)

Esiste documentazione posso guardare per riferimento? Non sono riuscito a trovarne.

+1

Come nell'obiettivo C. – Kevin

+0

Non riesco a trovare nulla con il tipo di animazione che desidero. Voglio solo una semplice slide in o fade in. Non è un gioco OpenGL. – Benr783

+0

Cerca i metodi animati di 'UIView'. Funzionano allo stesso modo in Swift come in Obj-C. –

risposta

15

Sono abituato a usare [UIView animateWithDuration ...], ma ho trovato un po 'complicato passare la sintassi del blocco a Swift all'inizio. Ecco un codice di pigro rapida:

view.alpha = 0.0 
UIView.animateWithDuration(0.25, animations: { 
    view.alpha = 1.0 
}, completion: { 
    (value: Bool) in 
    println(">>> Animation done.") 
}) 
+0

puoi dirmi come animare in modo continuo .. ?? –

+0

Forse prova qualcosa come https://stackoverflow.com/questions/28644335/swift-continuous-background-animation-loop – LordParsley

4

o per animare la posizione che appare dalla sinistra dello schermo:

// some variables for the UIView 
let width = 200 
let height = 100 
let yPosition = 10 

// create view and position it offscreen to the left  
let view = UIView() 

// you could have also used the new more verbose Swift way of making a CGRect: 
// CGRect(x: xValue, y: yValue, width: widthValue, height: heightValue) 
view.frame = CGRectMake(-width, yPosition, width, height) 
view.backgroundColor = UIColor.blueColor() // etc... 

// animation position over 1.0 second duration 
UIView.animateWithDuration(1.0, animations: { 

    view.frame = CGRectMake(0, yPosition, width, height) 
}) 

Se siete completamente nuovo per le API UIView animazione ho scritto post che copre una mucchio di opzioni di animazione in rapida qui: http://mathewsanders.com/prototyping-iOS-iPhone-iPad-animations-in-swift/

0

Swift 3 si prega di aggiungere gli oggetti necessari come tableView

import UIKit 

class SecondViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 


    @IBOutlet var tabl: UITableView! 
    var name = ["tony","abu"] 
    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
    func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
    } 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return name.count 
    } 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = UITableViewCell() 
     cell.textLabel?.text = name[indexPath.row] 
     return cell 
    } 
    override func viewWillAppear(_ animated: Bool) { 

     tabl.center.x = self.view.frame.width/5 

     UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity:0.45, options: [], animations: ({ 
      self.tabl.center.x = self.view.frame.width/3 
      //self.buttonAction.center.x = self.view.frame.width/2 
     }), completion: nil) 



    } 

} 
Problemi correlati