2013-04-13 16 views
5

Tôi muốn vẽ vùng trong suốt bằng cọ, nhưng mã của tôi không hoạt động tốt. Tôi nghĩ ai đó có thể giúp tôi ở đây. Mã của tôi:Tôi có thể điền vào vùng trong suốt của hình ảnh như thế nào khi ngón tay của tôi di chuyển trên

// Handles the start of a touch 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGRect bounds = [self bounds]; 
    UITouch *touch = [[event touchesForView:self] anyObject]; 

    if (![image isPointTransparent:[touch locationInView:self]] 
     || ![image isPointTransparent:[touch previousLocationInView:self]]) 
    { 
     return; 
    } 

firstTouch = YES; 

    // Convert touch point from UIView referential to OpenGL one (upside-down flip) 

location = [touch locationInView:self]; 
location.y = bounds.size.height - location.y; 
} 

// Handles the continuation of a touch. 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGRect bounds = [self bounds]; 
    UITouch *touch = [[event touchesForView:self] anyObject]; 

    if (![image isPointTransparent:[touch locationInView:self]] 
     || ![image isPointTransparent:[touch previousLocationInView:self]]) 
    { 
     return; 
    } 

// Convert touch point from UIView referential to OpenGL one (upside-down flip) 
if (firstTouch) 
    { 
    firstTouch = NO; 
     previousLocation = [touch previousLocationInView:self]; 
    previousLocation.y = bounds.size.height - previousLocation.y; 
} 
    else 
    { 
    location = [touch locationInView:self]; 
    location.y = bounds.size.height - location.y; 
    previousLocation = [touch previousLocationInView:self]; 
    previousLocation.y = bounds.size.height - previousLocation.y; 
} 

// Render the stroke 
[self renderLineFromPoint:previousLocation toPoint:location]; 
} 

// Handles the end of a touch event when the touch is a tap. 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGRect bounds = [self bounds]; 
    UITouch *touch = [[event touchesForView:self] anyObject]; 

    if (![image isPointTransparent:[touch locationInView:self]] || ![image isPointTransparent:[touch previousLocationInView:self]]) 
    { 
     return; 
    } 

    if (firstTouch) 
    { 
     firstTouch = NO; 
     previousLocation = [touch previousLocationInView:self]; 
     previousLocation.y = bounds.size.height - previousLocation.y; 
     [self renderLineFromPoint:previousLocation toPoint:location]; 
} 
} 
+1

Chính xác thì vấn đề của bạn là gì? Vui lòng giải thích vấn đề bạn gặp phải là gì, vì chúng tôi không thể đoán được bằng cách xem mã của bạn. Hãy cho chúng tôi biết điều gì xảy ra khi bạn chạy ứng dụng của mình. – klcjr89

+1

Có một video WWDC 2012 rất hay hiển thị cách "vẽ bằng ngón tay của bạn". – matt

+0

Tôi bắt đầu tìm hiểu Objective-c với ứng dụng vẽ , Suy nghĩ về một hình ảnh với khu vực alpha, và khu vực này có thể bất thường. Tôi chỉ muốn vẽ điểm trên khu vực alpha khi tôi di chuyển ngón tay của mình lên hình ảnh. – bencore

Trả lời

0

Điều quan trọng cần biết là bạn nên làm bản vẽ thực tế của bạn trong drawRect: của UIView của bạn. Vì vậy, các renderLineFromPoint:toPoint: phương pháp trong mã của bạn chỉ nên xây dựng một loạt các dây chuyền và nói với quan điểm để vẽ lại mỗi lần, một cái gì đó như thế này:

- (void)renderLineFromPoint:(CGPoint)from toPoint:(CGPoint)to 
{ 
    [lines addObject:[Line lineFrom:from to:to]]; 
    [self setNeedsDisplay]; 
} 

này giả định rằng bạn có một lớp đường trong đó có 2 CGPoint tài sản. drawRect: của bạn có thể trông giống như thế này:

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(context, 0.0f, 0.0f, 0.0f, 1.0f); 
    for (Line *line in lines) { 
    CGContextMoveToPoint(context, line.from.x, line.from.y); 
    CGContextAddLineToPoint(context, line.to.x, line.to.y); 
    CGContextStrokePath(context); 
    } 
} 

Nếu bạn làm điều đó như thế này (không có OpenGL) không có nhu cầu để lật trục y.

Điều duy nhất còn lại là triển khai phương thức isPointTransparent:. Rất khó để biết từ mã của bạn như thế nào điều này sẽ làm việc.

Các vấn đề liên quan