我使用滑动手势来更改主视图的外观:
@IBAction func changeSeqBack(_ recognizer: UISwipeGestureRecognizer) {
if (recognizer.direction == UISwipeGestureRecognizerDirection.right) {
count = 0
if timerIndex % 5 == 0 {
timerIndex -= 5
initialState -= 1
viewDidLoad()
} else {
while timerIndex % 5 != 0 {
timerIndex -= 1
if timerIndex % 5 == 0 {
count = 0
initialState -= 0
viewDidLoad()
}
}
}
}
}
initialState
变量(通过滑动更改)调用了许多隐藏或显示屏幕上各种对象的方法。问题是我只能在每次滑动后调用viewDidLoad()进行更改。这似乎是不好的做法,因为它加载了内存。还有其他方法可以让记忆变得容易吗?
答案 0 :(得分:4)
为初始设置创建一个新函数,并将viewDidLoad()中的代码移动到要刷新的函数,并从viewDidLoad()和refresh函数中调用它:
override viewDidLoad(){
super.viewDidLoad()
testFunction()
}
func testFunction() {
// write your code here
}
if (recognizer.direction == UISwipeGestureRecognizerDirection.right) {
count = 0
if timerIndex % 5 == 0 {
timerIndex -= 5
initialState -= 1
testFunction()
} else {
while timerIndex % 5 != 0 {
timerIndex -= 1
if timerIndex % 5 == 0 {
count = 0
initialState -= 0
testFunction()
}
}
}
}