0

我想在我看来经常画线,但它不起作用。我不想使用drawRect,因为我还必须保持先前绘制的线条的状态。下面是画线的代码。请指导。

- (void)drawPathWithPoints:(int)xAxis andYaxis:(int)yAxis
{    
    CGSize screenSize = drawingImgView.frame.size;

    UIGraphicsBeginImageContext(drawingImgView.frame.size);

    CGContextRef currentContext = UIGraphicsGetCurrentContext();
     [drawingImgView.image drawInRect:CGRectMake(0, 0, screenSize.width, screenSize.height)];

    CGContextSetLineCap(currentContext, kCGLineCapRound);
    CGContextSetLineWidth(currentContext, 9.0);
    CGContextSetRGBStrokeColor(currentContext, 0, 0, 1, 1);
    CGContextBeginPath(currentContext);

    CGMutablePathRef pointPath = CGPathCreateMutable();

    CGContextMoveToPoint(currentContext,xAxis,yAxis);

    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.duration = 3.0;
    pathAnimation.delegate = self;
    pathAnimation.calculationMode = kCAAnimationPaced;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;

    CGPathMoveToPoint(pointPath, NULL, xAxis, yAxis);
    CGContextAddLineToPoint(currentContext, xAxis, yAxis);
    CGPathAddLineToPoint(pointPath, NULL, xAxis, yAxis);

    pathAnimation.path = pointPath;

    myLayer = [[CAShapeLayer alloc] init];
    myLayer.strokeColor = [[UIColor greenColor] CGColor];
    myLayer.lineWidth = 11.0;
    myLayer.fillColor = nil;
    myLayer.lineJoin = kCALineJoinBevel;
    myLayer.path = pointPath;
    [drawingImgView.layer addSublayer:myLayer];

    CGPathRelease(pointPath);
}
4

3 回答 3

1
- (void)drawPathWithPoints:(int)xAxis andYaxis:(int)yAxis
{    
    CGSize screenSize = drawingImgView.frame.size;

    UIGraphicsBeginImageContext(drawingImgView.frame.size);

    CGContextRef currentContext = UIGraphicsGetCurrentContext();
     [drawingImgView.image drawInRect:CGRectMake(0, 0, screenSize.width, screenSize.height)];

    CGContextSetLineCap(currentContext, kCGLineCapRound);
    CGContextSetLineWidth(currentContext, 9.0);
    CGContextSetRGBStrokeColor(currentContext, 0, 0, 1, 1);
    CGContextBeginPath(currentContext);



    CGContextMoveToPoint(currentContext,xAxis,yAxis);

     CGContextAddLineToPoint(currentContext, xAxis, yAxis);
            CGContextStrokePath(UIGraphicsGetCurrentContext());

  drawingImgView.image = UIGraphicsGetImageFromCurrentImageContext();

    CGPathRelease(pointPath);
}
于 2013-07-04T06:40:19.600 回答
0

如果你想画线UIView然后使用touchesBegan:touchesMoved:方法..我用一个演示,代码如下...

#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    myPath = [[UIBezierPath alloc] init];
    myPath.lineWidth = 10;

    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    [myPath moveToPoint:[mytouch locationInView:self]];
    [pathArray addObject:myPath];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{    
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    [myPath addLineToPoint:[mytouch locationInView:self]];
    [self setNeedsDisplay];
}
于 2013-07-04T06:27:12.270 回答
0
#import <QuartzCore/QuartzCore.h>
#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self drawlineFromPoint:CGPointMake(10, 10) toPoint:CGPointMake(300, 300)];
}

- (void)drawlineFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint
{
    //1. Create bezier path from first point to second.
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:firstPoint];
    [path addLineToPoint:secondPoint];


    //2. Create a shape layer for above created path.
    CAShapeLayer *myLayer = [[CAShapeLayer alloc] init];
    myLayer.strokeColor = [[UIColor greenColor] CGColor];
    myLayer.lineWidth = 11.0;
    myLayer.fillColor = nil;
    myLayer.lineJoin = kCALineJoinBevel;
    myLayer.path = path.CGPath;
    [self.imageView.layer addSublayer:myLayer];


    //3. Animate the path
    CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    drawAnimation.duration            = 3.0;
    drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    drawAnimation.toValue   = [NSNumber numberWithFloat:1.0f];
    drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    [myLayer addAnimation:drawAnimation forKey:@"drawLineAnimation"];
}

@end

希望这可以帮助!

于 2013-07-04T06:37:49.023 回答