2011-04-15 44 views
3

大家好我是法國人,所以我嘲笑我的英語。所以我想製作一個像飛行控制的遊戲。當我從一個像飛機這樣的圖像畫線時,我希望飛機跟着那條線。我該怎麼做?代碼iphone圖像繪製線

+0

可能重複: //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

+1

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

回答

1
  1. 劃清界線,實施touchesBegan:touchedMove:touchedEndedtouchesCancelled:在您的視圖或視圖控制器,並建立使用觸摸點的路徑(CGPathRef)。
  2. 要使對象沿着線條移動,請創建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
+0

好的,你可以給第二部分的更多細節,它正是在這裏,我的問題是 – 2011-04-15 20:01:42

+0

@arvin Arabi:我的答案現在包含示例代碼,應該爲你工作。 – nubbel 2011-04-16 08:30:06

+0

我嘗試使用它,但我不能你可以使用一個例子或請給出整個示例代碼 – 2011-04-18 20:13:23