大家好我是法國人,所以我嘲笑我的英語。所以我想製作一個像飛行控制的遊戲。當我從一個像飛機這樣的圖像畫線時,我希望飛機跟着那條線。我該怎麼做?代碼iphone圖像繪製線
回答
- 劃清界線,實施
touchesBegan:
,touchedMove:
,touchedEnded
,touchesCancelled:
在您的視圖或視圖控制器,並建立使用觸摸點的路徑(CGPathRef)。 - 要使對象沿着線條移動,請創建CAKeyframeAnimation,設置路徑並將其分配給對象的圖層。
編輯:示例代碼
當你有一個CGPathRef path
,創建動畫一樣簡單:
CAKeyframeAnimation* animation = [CAKeyframeAnimation animation];
animation.path = thePath;
animation.duration = 2;
animation.rotationMode = kCAAnimationRotateAuto; // object auto rotates to follow the path
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
最後,分配動畫層:
[layer1 addAnimation:animation forKey:@"position"];
編輯2:示例應用程序:
我爲您構建了一個名爲PathAnimation的整個項目。
編輯3:這是我在PathAnimation項目中使用的代碼:
//
// CustomView.m
// PathAnimation
//
// Created by Dominique d'Argent on 19.04.11.
// Copyright 2011 Nicky Nubbel. All rights reserved.
//
#import "CustomView.h"
@implementation CustomView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
object = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow"]];
object.frame = CGRectMake(10.0, 30.0, 20.0, 20.0);
[self addSubview:object];
[self createPath];
}
return self;
}
- (void)dealloc
{
[object release];
[super dealloc];
}
#pragma mark - Custom drawing
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
CGContextRef g = UIGraphicsGetCurrentContext();
CGFloat bgColor[4] = {1.0f, 1.0f, 1.0f, 1.0f};
CGContextSetFillColor(g, bgColor);
CGContextFillRect(g, self.frame);
CGFloat color[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextAddPath(g,path);
CGContextSetStrokeColor(g, color);
CGContextDrawPath(g, kCGPathStroke);
CGPoint position = CGPathGetCurrentPoint(path);
CGContextAddArc(g, position.x, position.y, 5.0f, 0.0f, 2 * M_PI, 0);
CGContextSetFillColor(g, color);
CGContextDrawPath(g, kCGPathFill);
}
#pragma mark - Path creation
- (void)createPath {
path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, object.center.x, object.center.y);
}
#pragma mark - Touch handling
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint position = [touch locationInView:self];
if (CGRectContainsPoint(object.frame, position)) {
// start animation
[self go];
}
else {
CGPoint lastPosition = CGPathGetCurrentPoint(path);
if (CGPointEqualToPoint(lastPosition, object.center)) {
CGFloat angle = -atan2f(position.x - lastPosition.x, position.y - lastPosition.y);
angle += M_PI_2;
object.layer.transform = CATransform3DMakeRotation(angle, 0.0, 0.0, 1.0);
}
CGPathAddLineToPoint(path, NULL, position.x, position.y);
[self setNeedsDisplay];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
}
#pragma mark - Animations
- (void) go {
NSLog(@"go");
object.layer.transform = CATransform3DIdentity;
CAKeyframeAnimation* animation = [CAKeyframeAnimation animation];
animation.path = path;
animation.duration = 5.0;
animation.rotationMode = kCAAnimationRotateAuto; // object auto rotates to follow the path
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[object.layer addAnimation:animation forKey:@"position"];
object.center = CGPathGetCurrentPoint(path);
[self createPath];
}
@end
的[I如何可以動畫沿曲線路徑的圖或圖像的移動?](HTTP
好的,你可以給第二部分的更多細節,它正是在這裏,我的問題是 – 2011-04-15 20:01:42
@arvin Arabi:我的答案現在包含示例代碼,應該爲你工作。 – nubbel 2011-04-16 08:30:06
我嘗試使用它,但我不能你可以使用一個例子或請給出整個示例代碼 – 2011-04-18 20:13:23
- 1. 在靜態圖像地圖上繪製路線iphone
- 2. iphone中的繪製路線視圖
- 3. iPhone UIView調整和圖像繪製
- 4. 通過觸摸iPhone繪製圖像sdk
- 5. 繪製與iPhone的路線
- 6. 在iPhone中繪製曲線?
- 7. 廣東話繪製iphone線
- 8. 如何在代碼中繪製圖像視圖
- 9. 安卓在線繪製圖像
- 10. 用PyQt在圖像上繪製線條
- 11. 在圖像上繪製對角線
- 12. 沿着曲線繪製動畫圖像
- 13. 在拍攝圖像上繪製線條
- 14. 通過UIScrollView繪製圖像和線條
- 15. 繪製圖像
- 16. 繪製圖像?
- 17. 繪製圖像
- 18. 畫布不是在代碼中繪製圖像,而是在開發人員控制檯中繪製圖像
- 19. 如何在bb中的編碼圖像上繪製一條線?
- 20. 繪製R圖線
- 21. SAS:如何繪製箱線圖中的幾行代碼
- 22. 如何調用從C#代碼中繪製WPF圖像的本機代碼?
- 23. 圖像識別庫/爲iPhone代碼
- 24. Android從java代碼添加可繪製圖像?
- 25. 在Mac OS上優化2D圖像繪製代碼
- 26. iPhone的GPS地圖繪製
- 27. 在iPhone上繪製圖形
- 28. 從繪圖代碼
- 29. CGContext繪製圖像
- 30. 3D圖像繪製
可能重複: //sackoveroverflow.com/questions/1142727/how-can-i-animate-the-movement-of-a-view-or-image-along-a-curved-path) – 2011-04-18 17:43:43
vous devez vous rendreàchaque question que vous avezdéjàdemandéet cliquez sur la caseàcocher par laréponsela plusappareséeàl'acceptpter。 Aider les gensàobtenir la reconnaissance。 Si vous n'avez pas「accepter」lesréponsesen cochant les gens sont moins susceptibles d'aider。 Vous devez augmenter votre「accepter taux«aussi proche que possible de 100% – 2011-04-26 20:42:20