2011-09-22 58 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(); 
    }