如何使此代码更流畅.我正在尝试绘制三角形,只需一次触摸(移动触摸),我想调整它的大小



如何使此代码更流畅。我正在尝试绘制三角形,只需一次触摸(移动触摸),我想调整它的大小。这是我的代码:

-(id)initWithPoint:(CGPoint )point withFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        givenPoint = point;
    }
    return self;
    }
    - (void)drawRect:(CGRect)rect
    {
    // Drawing code
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 1);

    CGPoint points[6] = { CGPointMake(10, 10), CGPointMake(50, 10),
        CGPointMake(50, 10), givenPoint,
        givenPoint, CGPointMake(10, 10) };
    CGContextSetRGBFillColor(ctx, 255, 255, 255, 1);
    CGContextStrokeLineSegments(ctx, points, 6);

    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
    UIView *view = touch.view;
    [view removeFromSuperview];
    TailView *tailView = [[TailView alloc] initWithPoint:CGPointMake(location.x, location.y)  withFrame:CGRectMake(100, 100, location.x + 40, location.y + 40)];
    tailView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tailView];
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [tailView addGestureRecognizer:panRecognizer];
}

简而言之,drawRect很慢。如果你想要它平滑,你应该使用变换。应该应用缩放和旋转变换,而不是从超视图中删除视图并再次添加,从而强制重画。

最新更新