iOS項目開發實戰——實現視圖切換動畫

來源:互聯網
上載者:User

iOS項目開發實戰——實現視圖切換動畫

不同介面或者說不同視圖之間進行切換是應用程式的一種最常見的動態效果,無論是哪一種平台的項目開發,預設的視圖切換往往是十分單調的,沒有任何動畫的,介面的切換也是非常的突兀。如果說使用動畫效果使介面能夠活躍起來,那麼你的App將會非常動感。這裡將實現視圖切換過程中的動畫效果。具體實現如下:

(1)本次實驗將拖入2張圖片,不直接放到View Controller中,而是在代碼中動態載入。拖到Main.storyboard中後目錄結構如下:

 

(2)實現圖片與代碼Outlet綁定:

 

    @IBOutlet weak var image1: UIImageView!    @IBOutlet weak var image2: UIImageView!

(3)在ViewController中實現如下代碼:

 

 

import UIKitclass ViewController: UIViewController {        @IBOutlet weak var image1: UIImageView!    @IBOutlet weak var image2: UIImageView!        var isFirstPic:Bool = true    //判斷是否是第一張圖片;使2張圖片能迴圈切換;        override func viewDidLoad() {        super.viewDidLoad()        self.view.addSubview(image1)   //首先載入第一張圖片;            }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }        override func touchesBegan(touches: Set, withEvent event: UIEvent) {                func complete(v:Bool){     //每一次視圖切換後執行的操作;                    println(Already Complete )            isFirstPic = !isFirstPic        }                if(isFirstPic){   //第一張圖片切換到第二張圖片;                    UIView.transitionFromView(image1, toView: image2, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromLeft, completion: complete)             }        else{    //第二張圖片切換到第一張圖片;                    UIView.transitionFromView(image2, toView: image1, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromTop, completion: complete)                    }            }    }

(4)注意UIViewAnimationOptions是切換動畫的選項,可以看到裡面後多個選項,共有8種動畫效果:大家可以自己嘗試一下,總有一款適合你的。

 

.

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.