objective-c - UIBezierPath 绘制直线

标签 objective-c ios ipad uibezierpath

这是我目前正在使用的相关 .m。

- (void)drawRect:(CGRect)rect
{

    [[UIColor redColor] setStroke];
    for (UIBezierPath *_path in pathArray) 
    [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];    


}

#pragma mark -
#pragma mark - Touch Methods


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    myPath=[[UIBezierPath alloc]init];


    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

    if([ud objectForKey:@"lineThickness"] == nil) {
        myPath.lineWidth=5;
    }
    else {

        float thicknessFloat = [ud floatForKey:@"lineThickness"];
        myPath.lineWidth= 10. * thicknessFloat;


    }

    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];
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

    if([ud objectForKey:@"lineThickness"] == nil) {
        myPath.lineWidth=5;
    }
    else {

        float thicknessFloat = [ud floatForKey:@"lineThickness"];
        myPath.lineWidth= 10. * thicknessFloat;


    }
    [myPath addLineToPoint:[mytouch locationInView:self]];
    [self setNeedsDisplay];

}

效果很好,但由于这是我稍微修改过的教程代码,我不知道如何解决想要在两点之间画线的问题,并让框架每次连接点已添加。

谁能给我指出一个好的方向,告诉我如何完成这个?

最佳答案

如何实现它的细节取决于您正在寻找的效果。如果您只是点击一堆点并想将它们添加到 UIBezierPath,您可以在 View Controller 中执行如下操作:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    CGPoint location = [mytouch locationInView:self.view];

    // I'm assuming you have a myPath UIBezierPath which is an ivar which is 
    // initially nil. In that case, we'll check if it's nil and if so, initialize 
    // it, otherwise, it's already been initialized, then we know we're just
    // adding a line segment.

    if (!myPath)
    {
        myPath = [UIBezierPath bezierPath];
        [myPath moveToPoint:location];

        shapeLayer = [[CAShapeLayer alloc] initWithLayer:self.view.layer];
        shapeLayer.lineWidth = 1.0;
        shapeLayer.strokeColor = [UIColor redColor].CGColor;
        shapeLayer.fillColor = [UIColor clearColor].CGColor;

        [self.view.layer addSublayer:shapeLayer];
    }
    else
    {
        [myPath addLineToPoint:location];
        shapeLayer.path = myPath.CGPath;
    }
}

如果你想要一些可以用手指画的东西(例如拖动你的手指画),那么它可能看起来像:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    CGPoint location = [mytouch locationInView:self.view];

    myPath = [UIBezierPath bezierPath];
    [myPath moveToPoint:location];

    shapeLayer = [[CAShapeLayer alloc] initWithLayer:self.view.layer];
    shapeLayer.lineWidth = 1.0;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;

    [self.view.layer addSublayer:shapeLayer];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    CGPoint location = [mytouch locationInView:self.view];

    [myPath addLineToPoint:location];
    shapeLayer.path = myPath.CGPath;
}

关于objective-c - UIBezierPath 绘制直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12983211/

相关文章:

iphone - 使用 PayPal 的 iPhone/iPad 应用程序列表

objective-c - NSMenuDelegate 未为另一个 NSMenuDelegate 创建的 NSMenuItem 中的子菜单调用

objective-c - 如何自定义UIActionSheet?苹果系统

c++ - 为什么 __weak 属性需要运行时支持

php - iScroll.js 和 iOS 浏览器检测 - 仅向 iPad/iPhone 显示 iScroll.js 文件

ios - 如何为 iPad (iOS 8.1) 创建表单弹出窗口

ios - 从 User * 分配给 NSArray * 的不兼容指针类型

iphone - 类似于 App Store 特色横幅的旋转横幅

ios - 如何在 Swift 中更改工具栏颜色

ios - dequeueReusableCellWithIdentifier 不返回自定义单元格