这个社区在很多方面给了我很大的帮助。第一个问题(对我来说),这是一个简单的问题。我正在经历iPhone软件开发工具包的学习曲线,速度很快……但每隔一段时间,我就会遇到一个问题,尽管它很简单,但询问和处理其他事情要容易得多,而不是花一个小时阅读。
我有一个2D游戏,其中一辆车在表面上移动,旋转着面向行进的方向。我已经确定Core Animation是我最好的方法。这辆车是一幅图像。它与用户输入(触摸)是交互的。
我在正确的轨道上吗?包含CALayer树的UIView (用作响应器),其中包含图像(来自文件)。当前文件为GIF格式。它可以很容易地使框架透明,只留下车辆图像。
在UIView子类中,如何将gif图像加载到层中?
听起来很简单,所以我想...
干杯。
发布于 2009-07-22 13:22:16
有了Core Animation,您就走上了正确的道路。CAKeyFrameAnimation有一个path属性,您将广泛使用该属性。以下示例代码(未经测试)使用直线路径,但也可以使用曲线路径:
UIImage *carImage = [UIImage imageNamed:@"car.png"];
carView = [[UIImageView alloc] initWithImage:carImage];
[mapView addSubview:carView];
CAKeyframeAnimation *carAnimation = [CAKeyframeAnimation
animationWithKeyPath:@"position"];
carAnimation.duration = 5.0;
// keep the car at a constant velocity
carAnimation.calculationMode = kCAAnimationPaced;
// Rotate car relative to path
carAnimation.rotationMode = kCAAnimationRotateAuto;
// Keep the final animation
carAnimation.fillMode = kCAFillModeForwards;
carAnimation.removedOnCompletion = NO;
CGMutablePathRef carPath = CGPathCreateMutable();
CGPathMoveToPoint(carPath, NULL, 0.0, 0.0);
CGPathAddLineToPoint(carPath, NULL, 100.0, 100.0);
CGPathAddLineToPoint(carPath, NULL, 100.0, 200.0);
CGPathAddLineToPoint(carPath, NULL, 200.0, 100.0);
carAnimation.path = carPath;
CGPathRelease(carPath);
[carView.layer addAnimation:carAnimation forKey:@"carAnimation"];发布于 2009-07-21 16:35:19
有什么原因你不能简单地继承UIImageView来处理你的触摸方法吗?在我看来,使用您的图像实例化图像视图,并让被覆盖的UIResponder方法处理车辆移动的位置以及您需要的任何其他东西,将比手动管理CALayer树容易得多。
您可以使用类似于以下内容的内容来完成此操作:
UIImage *vehicleImage = [UIImage imageNamed:@"vehicle.gif"];
VehicleImageView *vehicleView = [[[VehicleImageView alloc]
initWithImage:vehicleImage] autorelease];然后让VehicleImageView子类UIImageView:
@interface VehicleImageView : UIImageView
// Your stuff
@end
@implementation VehicleImageView
// Your stuff
// UIResponder methods
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// Custom implementation
}
// Other touch methods...
@end不过,你似乎走在了正确的道路上,因为你确实需要在你的车辆的视图/视图层次结构中的某个地方使用UIResponder来实现触摸方法。
更多信息:
initWithImage:)imageNamed:)touchesEnded:withEvent:)https://stackoverflow.com/questions/1160189
复制相似问题