xcode 在 Swift 中动画对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24110706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:02:25  来源:igfitidea点击:

Animate objects in Swift?

iosxcodeanimationswift

提问by Benr783

In swift, I am transitioning an object in to the view, and I need it to slide in, or fade in. How can I obtain this type of animation in my program?

在 swift 中,我正在将一个对象转换到视图中,我需要它滑入或淡入。如何在我的程序中获得这种类型的动画?

I have all my view elements made programmatically without IB (Interface Builder)

我的所有视图元素都是在没有 IB(接口生成器)的情况下以编程方式制作的

Is there documentation I can look at for reference? I could not find any.

是否有我可以查看的文档以供参考?我找不到任何。

回答by LordParsley

I'm used to using [UIView animateWithDuration ...], but I found it a bit tricky switching the block syntax over to Swift at first. Here's a quick lazy code:

我习惯使用 [UIView animateWithDuration ...],但我发现一开始将块语法切换到 Swift 有点棘手。这是一个快速的懒惰代码:

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

回答by MathewS

Or to animate the position appearing from the left of the screen:

或者为出现在屏幕左侧的位置设置动画:

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

If you're completely new to the UIView animation APIs I wrote post that covers a bunch of animation options in swift here: http://mathewsanders.com/prototyping-iOS-iPhone-iPad-animations-in-swift/

如果您完全不熟悉 UIView 动画 API,我写了一篇文章,其中涵盖了一系列 swift 动画选项:http: //mathewsanders.com/prototyping-iOS-iPhone-iPad-animations-in-swift/

回答by Tony Franzis

Swift 3 & 4 Please Add necessary objects like tableView

Swift 3 & 4 请添加必要的对象,如 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)



    }

}