#08利用 page control,segmented control,button & gesture 更換內容

以JUMP少年週刊的三部卡通來做練習

把所需要的元件都先新增到畫面上,再拉IBAction &IBOutlet

新增所需要的陣列和變數

let jumpImages = ["火影忍者", "海賊王", "七龍珠"]let jumpLables = ["講述忍者世界裡圍繞著身為對手的漩渦鳴人與宇智波佐助兩人,與其他角色的經歷。","描述海賊蒙其·D·魯夫想要得到「ONEPIECE」和成為「海賊王」為夢想而出海向「偉大的航道」航行的海洋冒險故事。","以孩子悟空的成長歷程為中心,描述「冒險」、「努力」、「友情」、「戰鬥」的長篇冒險漫畫"]var index = 0

segmented control的IBAction

@IBAction func changeSegment(_ sender: UISegmentedControl) {index = sender.selectedSegmentIndexpage.currentPage = indexjumpImage.image = UIImage(named: jumpImages[index])jupmLabel.text = jumpLables[index]}

page control的IBAction

@IBAction func changePage(_ sender: UIPageControl) {index = sender.currentPagejumpImage.image = UIImage(named: jumpImages[index])segment.selectedSegmentIndex = indexjupmLabel.text = jumpLables[index]}

Button的IBAction

@IBAction func pre(_ sender: Any) {index = (index + jumpImages.count - 1) % jumpImages.countjumpImage.image = UIImage(named: jumpImages[index])segment.selectedSegmentIndex = indexpage.currentPage = indexjupmLabel.text = jumpLables[index]}@IBAction func next(_ sender: Any) {index = (index + 1) % jumpImages.countjumpImage.image = UIImage(named: jumpImages[index])page.currentPage = indexsegment.selectedSegmentIndex = indexjupmLabel.text = jumpLables[index

完成之後因為內容有重複的程式碼,可以再試著簡化程式碼另外做一個function

func change(){page.currentPage = indexjumpImage.image = UIImage(named: jumpImages[index])jupmLabel.text = jumpLables[index]segment.selectedSegmentIndex = index}

手勢元件記得要拉到圖片上

然後imageView的interaction的第一個選項要打勾 不然滑動也不會更換圖片

附上我的GitHub https://github.com/kaikai215/moveChange08

--

--