ios - 我可以在任意 UIView 变量上设置动画吗?

标签 ios swift animation uiview core-animation

过去,只要我处理已经属于 UIKit 框架一部分的属性,我就会很好地使用 UIView 动画内容。

最近,我创建了一个自定义 UIView 控件,它显示一些日历/计划行为并支持无限滚动。控制滚动位置的是一个简单的 NSDate 变量,我已将其声明为:

var position = NSDate().mondayMorning() {
    didSet {
        self.setNeedsDisplay()
    }
}

只要位置发生变化,事情就会正常进行。使用 UIPanGestureRecognizer 设置滚动甚至非常简单:

struct StartState { static var position:NSDate = NSDate() }
switch pan.state {
case .Began:
    StartState.position = self.position
case .Changed:
    let translation = pan.translationInView(self).y
    self.position = StartState.position - ((7 * 24).hours / self.cellWidth * translation)
case .Cancelled:
    self.position = StartState.position
default:
    break
}

当我尝试在 .Ended 案例中实现贴花运动时,棘手的部分就出现了。理想情况下,我想要某种 UIView.animationWithDuration(... .CurveEaseOut) 咒语,但尽管经过多次尝试和错误,我还是没有任何效果。在过去,我已经能够做我自己的自定义的东西,但在这些情况下,我搞乱了布局,所以我只会让我的动画 block 成为一个 layoutIfNeeded,但在这个例如,我想沿着曲线修改这个 NSDate 值并驱动后续的显示更新。由于找不到有效的方法,我只是求助于高中物理并做了自己的事情:

case .Ended:
    let translation = pan.translationInView(self).y
    let v = pan.velocityInView(self).y
    let a:CGFloat = -4000.0 // 4 pixels/millisecond, recommended on gavedev
    let travelTime = (v / a).abs.seconds
    let started = NSDate()
    let timer = Timer()
    timer.interval = 40.milliseconds
    timer.tick = {
        let elapsed = NSDate() - started
        if elapsed >= travelTime {
            timer.stop()
        }
        else {
            let decelDisplacement = (v.abs * CGFloat(elapsed.seconds.magnitude) + (a * 0.5 * CGFloat(elapsed.seconds.magnitude.squared))) * v.sign
            self.position = StartState.position - ((7 * 24).hours / self.cellWidth * (translation + decelDisplacement))
        }
    }
    timer.start()

这有效。但如果可以在这里利用 CoreAnimation 的东西,我愿意。我可以做到这一点吗?或者动画内容是全部投入还是全部投入?

最佳答案

我还没有找到一种方法可以在不涉及 CoreAnimation 的情况下使用 UIView 动画 API 执行此操作。您当然可以使用 CoreAnimation,Apple 提供了一些 nice example code for this 。还有an objc.io article about this topic

我确实找到了this solution这适用于 UIView 动画,但实际上 underneath accomplishes this behaviour using CoreAnimation正如自述文件中所建议的那样。

关于ios - 我可以在任意 UIView 变量上设置动画吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37014977/

相关文章:

ios - swift 非空函数应该在 guard let 中返回一个值

html - 动画占位符文本 : Go to side and loose opacity

python - Matplotlib 动画;以推送模式制作动画,不使用回调函数

ios - 使用 UISearchController 自定义 UISearchBar

iphone - 如何解析 iPhone 应用程序中的 HTML 子标签?

ios - 无法用存储的属性覆盖 'description'

css - 如何在动画中使用剪辑路径?

ios - iOS swift 上的 SIGABRT ABORT 崩溃

ios - 从服务器检索数据后向上滚动时无限滚动 UITableView 出现故障

ios - 在 iOS 上获取通知主体?