而不是集中於UIButtons
和他們的活動,我刪除了UIButtons
,並專注於touchesBegan
,touchesMoved
和touchesEnd
方法。我將CGContext
方法合併爲僅在特定初始位置和特定當前位置註冊UITouch
時畫線。雖然我不確定這是否是對我的問題最理想/最有效的答案,但它運作良好,並使我能夠進入項目的下一個階段。如果有人有改進這個解決方案的建議,將不勝感激。以下是我用於此解決方案的代碼片段:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if([touch tapCount] == 2) {
image1_.image = nil;
return;
}
initialPoint = [touch locationInView:self.view];
//If initial touch to screen is within 15 pixels of Dot 1, set coordinates to Dot 1
if(initialPoint.x > 36 && initialPoint.x < 66 && initialPoint.y > 161 && initialPoint.y < 191)
{
initialPoint.x = 51.0;
initialPoint.y = 176.0;
}
//If initial touch to screen is within 15 pixels of Dot 2, set coordinates to Dot 2
if(initialPoint.x > 199.5 && initialPoint.x < 229.5 && initialPoint.y > 170.5 && initialPoint.y < 190.5)
{
initialPoint.x = 214.5;
initialPoint.y = 175.5;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self.view];
//If current touch to screen is within 15 pixels of Dot 2, and the initial touch is
//set to Dot 1, draw line
if(currentPoint.x > 199.5 && currentPoint.x < 229.5 && currentPoint.y > 170.5 && currentPoint.y < 190.5 && initialPoint.x == 51.0 && initialPoint.y == 176.0)
{
currentPoint.x = 214.5;
currentPoint.y = 175.5;
UIGraphicsBeginImageContext(self.view.frame.size);
[image1_.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width,
self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapSquare);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 4.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), initialPoint.x, initialPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
image1_.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lineIsDrawn = YES;
}
//If current touch to screen is within 15 pixels of Dot 3, and the initial touch is
//set to Dot 2, and a line has already been drawn between Dot 1 & Dot 2, draw line
if(currentPoint.x > 155.5 && currentPoint.x < 180.5 && currentPoint.y > 0 && currentPoint.y < 28.5 && initialPoint.x == 214.5 && initialPoint.y == 175.5 && lineIsDrawn == YES)
{
currentPoint.x = 170.5;
currentPoint.y = 13.5;
UIGraphicsBeginImageContext(self.view.frame.size);
[image1_.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width,
self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapSquare);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 4.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), initialPoint.x, initialPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
image1_.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
感謝您的鏈接,巧合的是,我參考了我的應用程序的示例。有誰會知道有關UIButton Sent Events描述的博客/參考嗎? – des253