如何更改UIBezierPath绘图线的形状?

时间:2016-01-27 06:01:40

标签: ios objective-c calayer uibezierpath

有没有办法改变UIBezierPath绘图形状,当用户拖动手指时,请看下面的图像就像一条线,但是我想要星形,圆形等可以实现这一点。

enter image description here

我的期望是 enter image description here

这是我的UIBezierPath代码:

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [[event allTouches] anyObject];
        touchPoint = [touch locationInView:self];

        if (!CGPointEqualToPoint(startingPoint, CGPointZero))
        {
            UIBezierPath *path = [UIBezierPath bezierPath];
            [path moveToPoint:CGPointMake(touchPoint.x,touchPoint.y)];
            [path addLineToPoint:CGPointMake(startingPoint.x,startingPoint.y)];

            CAShapeLayer *shapeLayer = [CAShapeLayer layer];
            shapeLayer.path = [path CGPath];
            shapeLayer.strokeColor = [single.arrColor[single.i] CGColor];
            if([UIDevice currentDevice].userInterfaceIdiom ==UIUserInterfaceIdiomPad)
            {
                shapeLayer.lineWidth = 7.0;

            }
            else
            {
                shapeLayer.lineWidth = 5.0;

            }
            shapeLayer.fillColor = [[UIColor redColor] CGColor];
            [self.layer addSublayer:shapeLayer];
            [clearBeizer addObject:shapeLayer];
        }
        startingPoint=touchPoint;
        //    [arrLayer addObject:shapeLayer];
        NSLog(@"Touch moving point =x : %f Touch moving point =y : %f", touchPoint.x, touchPoint.y);
    }

3 个答案:

答案 0 :(得分:1)

是的,这是可行的但不是微不足道的。你真正想要的是用星星而不是普通的破折号来描边。据我所知,iOS仅提供标准方法的API,即用矩形短划线模式进行描边。

如果要实现自定义描边,则必须自己动手。你可能必须首先压平bezier路径。然后&#34;步行&#34;沿路径并手动以一定的间隔绘制星星/圆/松鼠。如果您需要星星之间的间隔相等,则尤其困难。

您可以查看适用于MacOS的DrawKit库。 DrawKit适用于MacOS,而非iOS!这只是您获得想法的参考。

DrawKit在NSBezierPath+Geometry.h类上有NSBezierPath个类别。您可以从(NSBezierPath*)bezierPathWithZig:(CGFloat)zig zag:(CGFloat)zag方法开始,看看如何实现zig-zagy路径

https://github.com/DrawKit/DrawKit/.../NSBezierPath-Geometry.m#L1206

或波浪路径[(NSBezierPath*)bezierPathWithWavelength:amplitude:spread:]

https://github.com/DrawKit/DrawKit/..../NSBezierPath-Geometry.m#L1270

仅供参考:UIBezierPath(iOS)通常缺少NSBezierPath(MacOS)中可用的方法

如果DrawKit让您感到困惑,那么互联网上的iOS可能是开源的绘图库,请尝试搜索它们并查看自定义绘图是如何完成的。

答案 1 :(得分:1)

是的你可以做到这一点。但是你必须得到这个任务的自定义形状图标。

您可以尝试RobMayoff here:git repo

提供的优秀答案

这是另一种方法:

我制作了一个类似于你正在做的简单的图像编辑应用程序。

enter image description here

您可以在图像上绘制心形:

enter image description here

像这样,您可以绘制许多自定义形状:

enter image description here

代码非常简单明了:

您需要创建一些自定义形状的橡皮擦。我称它们为橡皮擦,因为它们只是擦掉了图片:P。

以下是自定义橡皮擦的方法:

- (void)newMaskWithColor:(UIColor *)color eraseSpeed:(CGFloat)speed {
    wipingInProgress = NO;
    eraseSpeed = speed;  //how fast eraser should move
    maskColor = color; //eraser color
    [self setNeedsDisplay];
}
-(void)setErase:(UIImage *)img{
eraser =img; //set the custom shaped image here
} 

在视图上绘制自定义形状的橡皮擦:

   - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        wipingInProgress = YES;
    }
   - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        if ([touches count] == 1) {
            UITouch *touch = [touches anyObject];
            location = [touch locationInView:self];
            location.x -= [eraser size].width/2;
            location.y -= [eraser size].width/2;
            [self setNeedsDisplay];
        }  
    }

最后是draw rect方法:

   - (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    if (wipingInProgress) {
        if (imageRef) {
            // Restore the screen that was previously saved
            CGContextTranslateCTM(context, 0, rect.size.height);
            CGContextScaleCTM(context, 1.0, -1.0);

            CGContextDrawImage(context, rect, imageRef);
            CGImageRelease(imageRef);

            CGContextTranslateCTM(context, 0, rect.size.height);
            CGContextScaleCTM(context, 1.0, -1.0);
        }

        [eraser drawAtPoint:location blendMode:kCGBlendModeDestinationOut alpha:eraseSpeed];
    }
    // Save the screen to restore next time around
    imageRef = CGBitmapContextCreateImage(context);

}

以下是.h文件中声明的一些变量:

CGPoint     location;
CGImageRef  imageRef;
UIImage     *eraser;
BOOL        wipingInProgress;
UIColor     *maskColor;
CGFloat     eraseSpeed;

答案 2 :(得分:0)

我只是使用UIImageView完成此操作:

 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:single.arrimages[single.colorimages]]];
                imageView.frame = CGRectMake(touchPoint.x, touchPoint.y, 30,30);
                [self addSubview:imageView];

只需在用户触摸屏幕时添加图片。

获得用户触摸的x,y协调,并将其提供给uiimageview框架。

我希望它会对某人有所帮助。