拉一個ImageView到Main.storyboard , 然候選擇Image,
下接選單中會看到剛才拉進來的圖片名稱
執行一下
接下來我們用程式碼來改變圖片, 再拉一張圖片進來
給ImageView一個名字,叫做myImage , 再拖一個UIButton進來
接下來寫程式,按下Button後會切換成另一張圖片
// // ViewController.swift // imageview // // Created by boywhy chen on 2016/12/17. // Copyright © 2016年 boywhy chen. All rights reserved. // import UIKit class ViewController: UIViewController { @IBOutlet weak var myImage: UIImageView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } var i=1 @IBAction func changeImage(sender: AnyObject) { if (i % 2 == 1 ) { myImage.image=UIImage(named: "P1120236.jpg") } else { myImage.image=UIImage(named: "P1120242.jpg") } i=i+1 } }
效果
沒有留言:
張貼留言