2014-06-08 19 views
5

Trong nhanh chóng, tôi chuyển một đối tượng vào xem, và tôi cần nó để trình chiếu trong, hoặc mờ dần trong. Làm thế nào tôi có thể có được loại hoạt hình trong chương trình của tôi?Làm động các đối tượng trong Swift?

Tôi có tất cả các yếu tố quan điểm của tôi đã lập trình mà không IB (Interface Builder)

Có tài liệu tôi có thể nhìn vào để tham khảo? Tôi không thể tìm thấy bất kỳ.

+1

cách tương tự như trong Objective C. – Kevin

+0

tôi không thể tìm thấy bất cứ điều gì với các loại hình hoạt hình mà tôi muốn. Tôi chỉ muốn có một slide đơn giản hoặc mờ dần. Không phải là một trò chơi OpenGL. – Benr783

+0

Tra cứu các phương thức animate của 'UIView'. Họ làm việc theo cách tương tự trong Swift như họ đã làm trong Obj-C. –

Trả lời

15

Tôi đang sử dụng để sử dụng [UIView animateWithDuration ...], nhưng tôi tìm thấy nó một chút khéo léo chuyển đổi cú pháp ngăn chặn hơn để Swift lần đầu tiên. Dưới đây là một mã lười biếng nhanh:

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

bạn có thể xin cho tôi biết làm thế nào để động liên tục .. ?? –

+0

Có lẽ thử một cái gì đó giống như https://stackoverflow.com/questions/28644335/swift-continuous-background-animation-loop – LordParsley

4

Hoặc để animate vị trí xuất hiện từ bên trái của màn hình:

// 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) 
}) 

Nếu bạn hoàn toàn mới để các API UIView hoạt hình tôi đã viết bài đó bao gồm một loạt các tùy chọn hình ảnh động trong nhanh chóng ở đây: http://mathewsanders.com/prototyping-iOS-iPhone-iPad-animations-in-swift/

0

Swift 3 hãy Thêm các đối tượng cần thiết như 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) 



    } 

} 
Các vấn đề liên quan