#4 日本近代文豪超簡介_アプリ開発初心者

這篇作業是為了練習利用 page control,segmented control,button & gesture 更換內容。

因為完全沒有頭緒一開始就先一股腦的參考(就是抄襲)學長姐的作法。看完程式碼後以及物件的配置後,鎖定好目標,做一個可以切換日本近代文豪作家介紹的頁面。

整個做好一個段落時,發現了二個問題:

①滑動照片沒有順利切換(Swipe Gesture Recognizer)
②Segmented Control按下去沒有反應。

首先先解決第一個問題參考了彼得潘大大的文章教學後,原來是沒有成功把gesture recognizer 跟image view連在一起。

第二個問題是因為把參數名搞錯了XD
以下為程式碼,等有空時再來好好說明程式碼的內容。

//
// ViewController.swift
// JapaneseGreatWriter
//
// Created by apple on 2021/7/20.
//

import UIKit

let writerImage = ["Natsume_Soseki_photo.jpeg","Akutagawa_Ryunosuke.jpeg","Kawabata_Yasunari.jpeg"]
let writerName = ["夏目漱石","芥川龍之介","川端康成"]
let page = ["『吾輩は猫である』","『羅生門』","『雪国』"]

var arrayIndex:Int = 0

class ViewController: UIViewController {

@IBOutlet weak var writerView: UIImageView!

@objc func changePage(_ sender: UISwipeGestureRecognizer){

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

@IBOutlet weak var writerNameLabel: UILabel!
@IBOutlet weak var writerPage: UIPageControl!

@IBOutlet weak var numberOfPage: UILabel!

@IBOutlet weak var timeSegment: UISegmentedControl!

func sync(){
writerView.image = UIImage(named: writerImage[arrayIndex])
writerPage.currentPage = arrayIndex
writerNameLabel.text = writerName[arrayIndex]
numberOfPage.text = page[arrayIndex]

timeSegment.selectedSegmentIndex = arrayIndex

}

@IBAction func pageControllerChange(_ sender: Any) {
if writerPage.currentPage == 0{
arrayIndex = 0
sync()
}
else if writerPage.currentPage == 1{
arrayIndex = 1
sync()
}
else {
arrayIndex = 2
sync()
}
}

@IBAction func leftBtn(_ sender: Any) {
if arrayIndex == 0{
arrayIndex = arrayIndex + 2
sync()
}
else if arrayIndex == 1{
arrayIndex = arrayIndex - 1
sync()
}
else{
arrayIndex = arrayIndex - 1
sync()
}
}

@IBAction func rightBtn(_ sender: Any) {
if arrayIndex == 0{
arrayIndex = arrayIndex + 1
sync()
}
else if arrayIndex == 1{
arrayIndex = arrayIndex + 1
sync()
}
else{
arrayIndex = arrayIndex - 2
sync()
}
}

@IBAction func timesChange(_ sender: Any) {
if timeSegment.selectedSegmentIndex == 0{
arrayIndex = 0
sync()
}
else if timeSegment.selectedSegmentIndex == 1{
arrayIndex = 1
sync()
}
else{
arrayIndex = 2
sync()
}

}

@IBAction func swipeShowPrevPic(_ sender: Any) {
if arrayIndex == 0{
arrayIndex += 2
sync()
}
else if arrayIndex == 1{
arrayIndex -= 1
sync()
}
else{
arrayIndex -= 1
sync()
}

}

@IBAction func swipeShowNextPic(_ sender: Any) {
if arrayIndex == 0{
arrayIndex += 1
sync()
}
else if arrayIndex == 1{
arrayIndex += 1
sync()
}
else{
arrayIndex -= 2
sync()
}
}
}

參考資料:

--

--