#6 運用 button 、 gesture 、page control更換內容|進擊的巨人

作品截圖

在 StoryBoard 製作,運用到的元件有:

  • Image View:放置圖片
  • Page Control:小圓點換頁
  • Segmented Control:Segment 換頁
  • Button:下一個/上一個換頁
  • Text View:文字顯示
  • Swipe Gesture Recognizer:左右滑動圖片換頁
import UIKit

class ViewController: UIViewController {
let photos = ["憲兵團","調查兵團","駐屯兵團"]
let introductions = [
"""
在國王身邊管理人民,人類陣營中的保守派。維持秩序。因為擁有在內地生活的權利,而且只有在訓練兵團的期末考試綜合成績前十名的新兵才能加入憲兵團,所以是幾乎所有新兵的終極目標。總人數大約2000名左右,如果加上歸憲兵團指揮的駐屯兵團,實際可動員兵力大約有5000名。在性質上跟當初以民主方式成立的調查兵團很容易產生對立。
""",

"""
抱著必死的決心挑戰牆外巨人領域。因為死亡率最高,所以是一般新兵絕不願意加入的兵團。人類陣營中的革命派。士兵精銳度遠非其他兵團可比,因為長期在前線與巨人戰鬥,所以幾乎人人都具有非凡的戰鬥能力。新兵第一次參加牆外遠征的死亡機率是五成,能夠撐過來的人將會逐漸成為生存機率高的優秀士兵。
""",
"""
負責強化城牆,防守各個城市,人類陣營中的中立派。考不上憲兵團,又不想加入調查兵團的新兵的最終去處。除了兩次巨人攻城戰外,基本沒有和巨人戰鬥過,所以對於害怕和巨人戰鬥的新兵來說也是個不錯的選擇。隸屬於憲兵團管轄,在駐紮兵團累積了不少經驗的士兵也可以申請轉調至憲兵團。
"""
]
//先拉Outlet
var index = 0//宣告index 初始為0
@IBOutlet weak var titanSegmentedControl: UISegmentedControl!
@IBOutlet weak var titanImageView: UIImageView!
@IBOutlet weak var introLabel: UILabel!
@IBOutlet weak var titanPageControl: UIPageControl!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

}
//同步Function
func updateUI() {
titanImageView.image = UIImage(named: photos[index])
introLabel.text = introductions[index]
titanSegmentedControl.selectedSegmentIndex = index
titanPageControl.currentPage = index
}
//在拉Action
@IBAction func selectphoto(_ sender: Any) {
index = titanSegmentedControl.selectedSegmentIndex
updateUI()
}


@IBAction func changePageControl(_ sender: Any) {
index = titanPageControl.currentPage
updateUI()
}

@IBAction func pre(_ sender: Any) {
index = (index + albums.count - 1) % photos.count
updateUI()

}

@IBAction func next(_ sender: Any) {
index = (index + 1) % photos.count
updateUI()
}

}

成品短片

GitHub:

參考資料:

https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/%E7%9C%9F%E6%84%9B%E7%9A%84%E6%A8%A1%E6%A8%A3-image-view-%E5%92%8C-segmented-control-%E7%B7%B4%E7%BF%92-675d64d5c94c

--

--

邱奕軒/Charlie
彼得潘的 Swift iOS / Flutter App 開發教室

IOS 初心者 Resolve to perform what you ought. Perform without fail what you resolve