我正在尝试将UIMotionEffect应用于GLKView子类上的自定义属性。这是我在视图设置上的代码:
UIInterpolatingMotionEffect *horizontalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"customCenter.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
horizontalMotionEffect.minimumRelativeValue = @(-50);
horizontalMotionEffect.maximumRelativeValue = @(50);
[self addMotionEffect:horizontalMotionEffect];该属性定义为:
@property (nonatomic) CGPoint customCenter;但是当我在动画循环中记录该属性时,它的al值为0。我遗漏了什么?
发布于 2016-08-19 00:14:05
我一直在寻找这个问题的答案,并自己找到了解决方案。
我想让SCNNode具有动画效果,但对于任何其他自定义对象来说,这应该很容易完成。
我创建了UIMotionEffect的子类并覆盖了keyPathsAndRelativeValuesForViewerOffset(viewerOffset: UIOffset) -> [String : AnyObject]?。我的子类是用SCNNode初始化的,这样它就可以在倾斜手机时修改它的属性。这样就可以为不可设置动画的属性设置动画。
下面是我的快速代码:
class SCNNodeTiltMotionEffect: UIMotionEffect {
var node: SCNNode? // The object you want to tilt
var baseOrientation = SCNVector3Zero
var verticalAngle = CGFloat(M_PI) / 4
var horizontalAngle = CGFloat(M_PI) / 4
init(node: SCNNode) {
super.init()
self.node = node // Set value at init
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func keyPathsAndRelativeValuesForViewerOffset(viewerOffset: UIOffset) -> [String : AnyObject]? {
// Set any properties of your object with values of viewerOffset attributes
node?.eulerAngles = SCNVector3Make(baseOrientation.x, baseOrientation.y + Float(viewerOffset.horizontal * horizontalAngle), baseOrientation.z - Float(viewerOffset.vertical * verticalAngle))
return nil
}
}如果要对可设置动画的属性进行动画处理,则应返回一个包含密钥路径和值的字典,或者在the official documentation中使用UIInterpolatingMotionEffect,了解更多详细信息
https://stackoverflow.com/questions/23720699
复制相似问题