1

How can I make this code smoother. I am trying to draw triangle and with one touch (touches moved) i want it resize. Here is my code:

-(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];

}
4

1 回答 1

0

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

于 2012-08-28T13:35:58.963 回答