【发布时间】:2018-11-22 01:49:00
【问题描述】:
我想对屏幕内容的更新进行动画处理,就像过渡到不同的视图控制器一样。例如,想象一个包含今天事件的日历页面场景。向左滑动会将内容更新为明天的事件,但视图控制器和场景是相同的。不过,我想为从右侧滑入的新内容制作动画。
我尝试将整个视图向左移动。这很有效,但它在下面显示了一个黑屏,虽然很快被正确的内容替换了,但还是很刺耳。
我记得在 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()
}
【问题讨论】:
-
“我尝试过将整个视图向左移动的动画效果” 请给我们看看这段代码好吗?
-
移动是对“向左滑动”手势的响应。这是手势处理代码和执行移动的函数
-
“我记得在 HyperCard 时代,您可以锁定卡片的显示,更新内容,然后通过转换解锁显示,例如溶解或擦除” iOS:apeth.com/iOSBook/ch17.html#_transitions 但是,任何内置转换都不太可能完全符合您的要求。
标签: swift4 ios11 xcode9 uiviewanimation