【问题标题】:diagonal lines from the top right trying to draw on a UIView右上角的对角线试图在 UIView 上绘制
【发布时间】:2017-12-06 20:05:44
【问题描述】:

我有一个名为 Slate 的平板电脑,我可以从中处理 x,y 点。我可以将这些 x,y 点传递给我的“drawShape”函数,相当于 touchesMoved 函数。我有一个为此目的设计的自定义 UIView 类,具有 4 个主要功能。前三个是touchesbegan/moved/ended,还有一个drawRect。

这是 UIView 子类的源代码...

//
//  DrawingLayerView.m
//
//
//  Created by Monica Kachlon on 12/3/17.
//

#import "DrawingLayerView.h"
#include "UIBezierPath+Interpolation.h"


@interface StrokeA : NSObject

@property (assign, nonatomic) CGPoint startPoint;

- (id)initWithStartPoint:(CGPoint)point;


@property (strong, nonatomic) NSMutableArray * points;

@end

@implementation StrokeA



- (id)initWithStartPoint:(CGPoint)point {
    self = [super init];
    self.startPoint = point;
    self.points = [NSMutableArray array];

    return self;
}


@end



@implementation DrawingLayerView

NSMutableArray * strokes;
NSMutableArray * points;


StrokeA * currentStroke;
UIBezierPath * currentPath;
UIBezierPath *path;
UIBezierPath *pathTwo;

UIBezierPath *Newpath;
CAShapeLayer * currentLayer;


- (void) noodlez
{
path = [UIBezierPath bezierPath];

   currentPath = [UIBezierPath bezierPath];

    pathTwo = [UIBezierPath bezierPath];

    Newpath = [UIBezierPath bezierPath];
     strokes = [NSMutableArray array];
     points = [NSMutableArray array];

}
- (void)startTouch: (CGPoint)point
{

    currentStroke = [[StrokeA alloc] initWithStartPoint:point];
    [strokes addObject:currentStroke];
}

- (UIBezierPath *)createPath {
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
    for (StrokeA * stroke in strokes) {
        [bezierPath moveToPoint:stroke.startPoint];
        for (NSValue * value in stroke.points) {
            [bezierPath addLineToPoint:[value CGPointValue]];
        }
    }
    return bezierPath;
}


- (void)endTouch: (CGPoint)point
{

    [points removeAllObjects];
    [self setNeedsDisplay];
}
- (void)drawShape: (CGPoint)point
{
    if (!currentStroke) {
        [self startTouch:point];
    }
    [currentStroke.points addObject:[NSValue valueWithCGPoint:point]];
    pathTwo = [self createPath];

    [self setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect {
    [[UIColor blackColor] setStroke];
    [pathTwo stroke];
}


@end

这是屏幕截图。

我试着写“好吗?”只是为了说明问题。

【问题讨论】:

标签: ios objective-c uiview


【解决方案1】:

尽管代码缺乏一些细节并且问题措辞不佳,但屏幕截图清楚地显示了路径的初始点在每种情况下都是 (0,0)。将路径中的点转储到控制台,您会看到它。弄清楚那些 (0,0) 是如何进入那里的,你就会解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-06
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2021-05-04
    • 2021-12-11
    • 2016-06-15
    相关资源
    最近更新 更多