我得到的是int SpriteKit。并想知道如何在SKNode对象上创建运动效果。
对于UIView,我使用以下方法:
+(void)registerEffectForView:(UIView *)aView
depth:(CGFloat)depth
{
UIInterpolatingMotionEffect *effectX;
UIInterpolatingMotionEffect *effectY;
effectX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x"
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
effectY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y"
type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
effectX.maximumRelativeValue = @(depth);
effectX.minimumRelativeValue = @(-depth);
effectY.maximumRelativeValue = @(depth);
effectY.minimumRelativeValue = @(-depth);
[aView addMotionEffect:effectX];
[aView addMotionEffect:effectY];
}我还没有为SKNode找到类似的东西。所以我的问题是这是可能的吗?如果不是,那么我如何实现它。
发布于 2016-03-19 13:00:52
UIInterpolatingMotionEffect在深层运行,你不能使用像"cloudX“这样的任意keyPath。即使在添加motionEffects之后,中心属性的实际值也不会改变。所以答案是,除了UIView,你不能添加其他的运动效果。使用特定属性之外的任意属性,如'center‘或'frame’也是不可能的。
发布于 2014-02-05 03:33:01
UIInterpolatingMotionEffect只是将设备倾斜映射到它所应用的视图的属性--这完全取决于您用什么keyPath来设置它,以及这些键路径的设置器做什么。
您发布的示例将水平倾斜映射到视图的center属性的x坐标。当设备水平倾斜时,UIKit会自动在视图上调用setCenter: (或者设置view.center =,如果您更喜欢这样的语法),传递一个X坐标与水平倾斜量成比例偏移的点。
您也可以在自定义UIView子类上定义自定义属性。由于您使用的是Sprite Kit,因此可以子类化SKView来添加属性。
例如..。假设你的场景中有一个云精灵,当用户倾斜设备时,你想要移动它。将其命名为SKScene子类中的一个属性:
@interface MyScene : SKScene
@property SKSpriteNode *cloud;
@end并在移动它的SKView子类中添加属性和访问器:
@implementation MyView // (excerpt)
- (CGFloat)cloudX {
return ((MyScene *)self.scene).cloud.position.x;
}
- (void)setCloudX:(CGFloat)x {
SKSpriteNode *cloud = ((MyScene *)self.scene).cloud;
cloud.position = CGPointMake(x, cloud.position.y);
}
@end现在,您可以创建一个keyPath为cloudX的UIInterpolatingMotionEffect,它应该*自动在场景中移动精灵。
(*完全未经测试的代码)
https://stackoverflow.com/questions/21552911
复制相似问题