代码iPhone图像绘制线条

3

大家好,我是法国人,我的英语可能不太好,请见谅。我想制作一个像飞行管制一样的游戏。当我从像飞机这样的图片上画出一条线时,我希望飞机能沿着这条线飞行。请问我该如何实现这个功能?


可能是重复的问题:如何沿着曲线路径动画移动视图或图像? - Brad Larson
1
您需要回到您之前提出的每个问题,并勾选最合适的答案以接受它。帮助他人获得认可。如果您没有勾选“接受”答案,那么别人就不太可能再帮助您了。您需要尽可能将自己的“接受率”提高到接近100%。 - user177800
1个回答

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

好的,你能否提供第二部分更详细的信息?我的问题就出在这里。 - arvin Arabi
@arvin Arabi:我的回答现在包含了示例代码,应该适用于你。 - nubbel
我尝试使用它,但是我无法做到,你能否给一个例子或者提供完整的示例代码? - arvin Arabi
@arvin Arabi:请下载我为你创建的项目,希望能有所帮助。 - nubbel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接