动画·第一个简单的iOS应用升级版

基础动画

基础动画是指在一个开始值和结束值之间制作动画。

alpha

给问题标签的 alpha (视图的透明度)属性添加动画。当用户切换到下一个问题时 ,使用动画让标签逐渐隐藏。

  • 可用的UIView方法
    class func animate(withDuration: TimeInterval, animations: () -> Void)
    使用指定的持续时间为一个或多个视图的更改添加动画效果。
    duration 是 NSTimeinterval 类型(它也属于 Double 类型) ,animations 是一个闭包。

  • 闭包语法

{( arguments ) -> return type in
      code
}

另一个标签

在界面上再添加一个标签,点击下一题时,现有的标签会渐渐消失 ,新的标签(显示了下 一题的文字)会渐渐出现。

import UIKit

//@IBAction
//标签和按钮
//重载:override func viewDidLoad()

class ViewController: UIViewController {

    //@IBOutlet var questionLable: UILabel! //@IBOutlet 关键字告诉 Xcode, 之后会使用 Interface Builder 关联插座变量。
    @IBOutlet var currentQuestionLabel:UILabel!
    @IBOutlet var currentQuestionLabelCenterXConstraint:NSLayoutConstraint!
    //该插座变量的为 NSLayoutConstraint 型,表明该插座变量用于关联 约束。
    @IBOutlet var nextQuestionLabel:UILabel!
    @IBOutlet var nextQuestionLabelCenterXConstraint:NSLayoutConstraint!
    
    @IBOutlet var answerLable: UILabel!
    //新添加的代码为所有 ViewController 对象增加了一个 questionlabel 插座变量和 answerlabel 插座变量。视图控制器可以使用一个插座变量对应一个特定的 UILabel 对象。
    let questions: [String] = ["From what is cognac made?",
                               "What is 7+7?",
                               "What is the capital of Vermont?"]
    let answers:[String] = [ "Montpelir","14", "Grapes"]
    
    var currentQuestionIndex: Int = 0
    
    @IBAction func showNextQuesion(sender: AnyObject){  //只有按下按钮后(@IBAction),该函数才会运行。
        currentQuestionIndex += 1  //用户每点击一次,currentQuestionIndex 就加 1。
        if currentQuestionIndex == questions.count{  //如果currentQuestionIndex = 3,currentQuestionIndex则置为0
            currentQuestionIndex = 0
        }

        let question: String = questions[currentQuestionIndex]
        print(currentQuestionIndex)  //显示 currentQuestionIndex 的值
        
        currentQuestionLabel.textAlignment = .center //让文字居中对齐。
        currentQuestionLabel.adjustsFontSizeToFitWidth = true  //让文字自适应标签的宽度。
//        questionLable.numberOfLines = 0
//        questionLable.lineBreakMode = NSLineBreakMode.byWordWrapping
        
        nextQuestionLabel.text = question
        
        answerLable.text = "???"
        
        animateLabelTransitions()
    
    }
    
    @IBAction func showAnswer(sender: AnyObject){
        let answer: String = answers[currentQuestionIndex]//此处 currentQuestionIndex 的值和 showNextQuesion 里的 currentQuestionIndex 值一样。
        print(currentQuestionIndex)  //这里的 currentQuestionIndex 值随上面的值的变化而变化。
        answerLable.textAlignment = .center
        answerLable.text = answer
    
    }
    
    
    func animateLabelTransitions() {  //动画实现函数
        
//闭包:用于功能实现
//        let animationClosure = {() -> Void in
//            self.questionLable.alpha = 1
//        }
        
//功能实现1
//缺陷:如果再点一次下一题,由于 nextQuestionlabel 的透明度已经是 1 了,所以不会有动画。
//        UIView.animate(withDuration: 0.5, animations: {
//            self.currentQuestionLabel.alpha = 0
//            self.nextQuestionLabel.alpha = 1
//        })
        
        //约束位置动画化
        view.layoutIfNeeded()  //强制更新界面布局
        let screenWidth = view.frame.width
        self.nextQuestionLabelCenterXConstraint.constant = 0
        self.currentQuestionLabelCenterXConstraint.constant += screenWidth
//功能升级2
//交换两个标签的引用关系。在动画完成后,设置 currentQuestionlabel 指向屏幕上的标签, nextQuestionlabel 指向隐藏的标签。
        //动画化标签透明度,使其达到淡入浅出的效果。
        UIView.animate(withDuration: 0.5,
                       delay: 0,
                       options: [],  //默认情况下,动画会使用渐入 渐出的时间方法。
                       //options: [.curveLinear],  //动画变为线性的,从头到尾都是相同的速度。
                       animations: {
                        self.currentQuestionLabel.alpha = 0
                        self.nextQuestionLabel.alpha = 1
                        
                        self.view.layoutIfNeeded()  //迫使视图根据最新的约束来布局其子视图
                       },
                       completion: {_ in
                        swap(&self.currentQuestionLabel, &self.nextQuestionLabel)
                        //使用 swap(_:_:) 方法可以将两个参数接收后再进行交换。
                        swap(&self.currentQuestionLabelCenterXConstraint,
                        &self.nextQuestionLabelCenterXConstraint)
                        self.updateOffScreenLabel()
                       }
                       )
        
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        nextQuestionLabel.alpha = 0
        
    }
    
    override func viewDidLoad(){
        //重载:应用启动之后,需要从数组中取出第一个问题,然后替换掉 questionlabexl 的占位符"???"。
        super.viewDidLoad()
        currentQuestionLabel.textAlignment = .center
        answerLable.textAlignment = .center
        currentQuestionLabel.text = questions[currentQuestionIndex]
        
        updateOffScreenLabel()  //当加载 ViewController 的视图时,将 nextQuestionLabel 移动到其屏幕外的位置。
        
        currentQuestionLabel.adjustsFontSizeToFitWidth = true  //让文字自适应标签的宽度。
        answerLable.text = "???"
    }
    
    func updateOffScreenLabel() {
        let screenWidth = view.frame.width
        nextQuestionLabelCenterXConstraint.constant = -screenWidth
        //根据storyboard设置的约束可知,父视图的中心约束位置是 nextQuestionLabelCenterXConstraint 和 currentQuestionLabelCenterXConstraint 的初始位置,初始位置默认的 constant 值为 0。

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 6,120评论 0 4
  • 公元:2019年11月28日19时42分农历:二零一九年 十一月 初三日 戌时干支:己亥乙亥己巳甲戌当月节气:立冬...
    石放阅读 6,930评论 0 2
  • 今天上午陪老妈看病,下午健身房跑步,晚上想想今天还没有断舍离,马上做,衣架和旁边的的布衣架,一看乱乱,又想想自己是...
    影子3623253阅读 2,939评论 3 8