我希望动画对sceen内容的更新,就像转换到其他视图控制器一样。例如,想象一下具有今天事件的日历页面的场景。向左滑动会将内容更新为明天的事件,但视图控制器和场景是相同的。不过,我还想从右边滑动新内容。
我尝试动画将整个视图向左移动。这样可行,但它会在下面显示一个黑色的屏幕,虽然很快就会被正确的内容取代但仍然很刺耳。
我记得在HyperCard时代,您可以锁定卡片的显示,更新内容,然后通过转换解锁显示,例如,溶解或擦除。我知道iOS可能不是这样做的,但我提到它只是为了帮助描述我想要做的事情。
添加做手势识别并移动的代码......
@objc func swipeDetected(gesture: UIGestureRecognizer) {
let calendar = Calendar.current
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case .left:
// move current page to the left
animateViewMoving(right: false, timeInterval: 0.3)
// update content
currentDate = calendar.date(byAdding: .day, value: 1, to: currentDate)!
populateSchedItems()
self.tableView.reloadData()
// return page with no duration
animateViewMoving(right: true, timeInterval: 0.0)
case .right:
// move page to left with no duration
animateViewMoving(right: false, timeInterval: 0.0)
// update content
currentDate = calendar.date(byAdding: .day, value: -1, to: currentDate)!
populateSchedItems()
self.tableView.reloadData()
// slide new content in from the left
animateViewMoving(right: true, timeInterval: 0.3)
default:
break
}
}
}
func animateViewMoving (toTheRight: Bool, timeInterval: Double) {
let frameWidth = self.view.frame.width
let movementDuration:TimeInterval = timeInterval
let movement:CGFloat = ( toTheRight ? frameWidth : -frameWidth)
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )
self.view.frame = self.view.frame.offsetBy(dx: movement, dy: 0)
UIView.commitAnimations()
}