第一次用户和iPhone开发者新手。我的问题集中在我的应用程序的体系结构而不是细节的代码。我的问题是:我是在正确的轨道上还是我需要重新考虑我对这部分应用程序的处理方法?
我正在尝试制作一个简单的“连接点”应用程序。我的应用程序具有徒手绘制触摸功能,我使用UIButtons来表示每个点。
我通过调用2个UIButtons(点)的中心属性来处理这个问题,如果开始/结束CGPoints是这两个点的中心坐标,则放置条件只绘制一条线。这不起作用!
所以我的问题是:
UIButtons是表示每个点的最佳方法吗?如果是这样,应该在每个点添加什么功能?它似乎是一个强有力的候选人,因为你可以调用中心属性并获得它的中心坐标。但是由于我遇到了这方面的问题,我认为单个像素可能不足以放置条件。
如果UIButtons不是表示每个点的最佳方法,那么什么是更好的替代方案?
最后,由于这个问题,我花了很多时间研究UIButton的属性和功能。我找不到通过UIButton提供的Sent Events选项描述的良好参考。有谁知道一个好的博客/参考?
提前感谢您的帮助。
答案 0 :(得分:0)
不推荐使用UIButton攻丝。这是一个通过触摸自由绘图的示例。
答案 1 :(得分:0)
我没有关注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();
}