2011-09-22 78 views
0

第一次用戶和iPhone Developer新手。我的問題集中在我的應用程序的體系結構上,而不是基本的代碼。我對你的問題是:我在正確的軌道上,還是我需要重新思考我對這部分應用程序的方法?iPhone開發 - 繪圖應用程序

我想做一個簡單的「連接點」應用程序。我的應用程序具有免費的手繪功能,我使用UIButtons來表示每個點。

我已經通過調用2個UIButtons(點)的中心屬性來解決這個問題,並且如果起點/終點CGPoints是這兩個點的中心座標,則只會繪製一條線條。這不行!

所以我的問題是:

是UIButtons表示每個點的最佳方法?如果是這樣,應該爲每個點添加哪些功能?這看起來像是一個強大的候選人,因爲您可以調用中心屬性並獲取它的中心座標。但是,由於我遇到了這個問題,我認爲一個像素可能不夠大而不能放置條件。

如果UIButtons不是表示每個點的最佳方法,那麼更好的選擇是什麼?

最後,我花了大量的時間研究UIButtons的屬性和功能,因爲這個問題。我無法找到對通過UIButton提供的Sent Events選項的描述的很好的參考。有誰知道一個好的博客/參考?

感謝您的幫助提前。

回答

0

而不是集中於UIButtons和他們的活動,我刪除了UIButtons,並專注於touchesBegantouchesMovedtouchesEnd方法。我將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(); 
    }