iOS項目開發實戰——製作視圖的平移動畫以及解決移動異常問題

來源:互聯網
上載者:User

iOS項目開發實戰——製作視圖的平移動畫以及解決移動異常問題

今天嘗試做了一個視圖的平移動畫,碰到一些問題,現在貼出來和大家分享。通過動畫效果,可以使我們的App更加的好看,增加使用者體驗。具體實現如下:

(1)在介面中拖入一個View控制項,設定成正方形,並進行填充顏色。然後綁定到代碼中;

(2)在類中重寫一個viewDidAppear()方法,當介面出現的時候開始執行動畫。

 

import UIKitclass PositionViewController: UIViewController {        @IBOutlet weak var greenSquare: UIView!            override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view.    }        override func viewDidAppear(animated: Bool) {        UIView.animateWithDuration(1, animations: {        //這裡是一個Closure,也就是一個閉包函數;            //平移到X軸上對稱的位置;            self.greenSquare.center.x = self.view.bounds.width - self.greenSquare.center.x                                })    }            override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }    }

(3)最後運行程式,綠色色塊成功平移,符合我們的預期。

 

 

可能色塊有時平移出現異常,注意不要勾選“Use Auto Layout”,不進行自動布局。此時能在Size Inspector中出現Autoresizing,Autoresizing是Auto Layout出現之前的一種介面布局方式。這樣View的平移就能正常顯示了。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.