2012-10-01 17 views
0

我正在製作一個應用程序,用戶可以在其中使用觸摸在圖像視圖中填充和描邊顏色。但我希望當用戶在圖像視圖範圍內填充顏色時,如果意外觸及它,他/她不應該將該顏色從該圖像視圖中溢出。直到現在我已經實現了。有什麼建議麼?在着色書型應用程序中爲imageview設置邊界

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    mouseSwiped = NO; 
    UITouch *touch = [touches anyObject]; 
    lastPoint = [touch locationInView:self.view]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

// mouseSwiped = YES; 
// UITouch *touch = [touches anyObject]; 
// CGPoint currentPoint = [touch locationInView:self.view]; 
//  
// UIGraphicsBeginImageContext(self.view.frame.size); 
// [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
// CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
// CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
// CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
// CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 
// CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); 
// CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); 
//  
// CGContextStrokePath(UIGraphicsGetCurrentContext()); 
// self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
// [self.tempDrawImage setAlpha:opacity]; 
// UIGraphicsEndImageContext(); 
//  
// lastPoint = currentPoint; 

    if(CGRectContainsPoint([testImage bounds], lastPoint)) 
    { 
     mouseSwiped = YES; 
     UITouch *touch = [touches anyObject]; 
     CGPoint currentPoint = [touch locationInView:self.view]; 

     UIGraphicsBeginImageContext(self.view.frame.size); 
     [self.testImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); 
     CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); 

     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     self.testImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     [self.testImage setAlpha:opacity]; 
     UIGraphicsEndImageContext(); 

     lastPoint = currentPoint; 
    } 
    else 
    { 
     // CGRectContainsPoint(testImage.frame, lastPoint) = ; 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    if(!mouseSwiped) { 
     UIGraphicsBeginImageContext(self.view.frame.size); 
     [self.testImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity); 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     CGContextFlush(UIGraphicsGetCurrentContext()); 
     self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 

    UIGraphicsBeginImageContext(self.mainImage.frame.size); 
    [self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0]; 
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity]; 
    self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    self.tempDrawImage.image = nil; 
    UIGraphicsEndImageContext(); 
} 
+0

你有答案嗎? – Alfa

回答

0

如果您在創建視圖的邊界的路徑並將其設置爲您的視圖的clipping path,我覺得應該沒什麼外面再這樣了,一旦設定畫會發生什麼。