我一直在寻找了几个小时一个答案,但很难找到的话题什么。
我有关于Objective-C的一个问题。 我在做一个应用程序,其中一个UIView检查来自用户触摸并且如果用户触摸和移动他/她的手指,用UIBezierPath路径绘制。 如果用户绘制,使路径贯穿本身应该从屏幕上消失。 当用户完成绘制的图案,从路径中的最后一个点的直线应与自动路径中的第一点连接(我用这个方法“closePath”),如果该行与其他“线相交“在路径的路径也应该从屏幕上消失。
我保存在我存储在另一个类中调用线为点A和点B.然后我节省了“线”,以一个NSMutableArray所谓的“线”,一个CGPoint每一个接触点。 每一个时间点被添加到路径I检查,看看是否收到得出点与点之间的线路通过使用一种方法(与任何在线“行”的相交 - (BOOL)checkLineIntersection:(CGPoint)p1所:(CGPoint)P2:(CGPoint)P3:(CGPoint)P4)我从本教程“得到http://www.iossourcecode.com/2012/08/02/how-to-make-a-game-like-切的绳部分-2 /”。
问题
问题是,当我运行该应用程序它的工作原理,但有时有时当我画等都以线条相交的路径不会消失。 我想不通为什么...这似乎为它,当我画慢慢发生更频繁。
代码:
MyView.h:
#import <UIKit/UIKit.h>
#import "Line.h"
@interface MyView : UIView {
NSMutableArray *pathArray;
UIBezierPath *myPath;
NSMutableArray *lines;
Line *line;
}
@end
MyView.m:
#import "MyView.h"
@implementation MyView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
pathArray=[[NSMutableArray alloc]init];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[[UIColor redColor] setStroke];
[[UIColor blueColor] setFill];
for (UIBezierPath *_path in pathArray) {
//[_path fill];
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
}
#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
myPath = [[UIBezierPath alloc]init];
lines = [[NSMutableArray alloc]init];
myPath.lineWidth=1;
UITouch *mytouch = [[event allTouches] anyObject];
[myPath moveToPoint:[mytouch locationInView:mytouch.view]];
[pathArray addObject:myPath];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if(myPath.isEmpty) {
} else {
UITouch *mytouch = [[event allTouches] anyObject];
[myPath addLineToPoint:[mytouch locationInView:mytouch.view]];
CGPoint pointA = [mytouch previousLocationInView:mytouch.view];
CGPoint pointB = [mytouch locationInView:mytouch.view];
line = [[Line alloc]init];
[line setPointA:pointA];
[line setPointB:pointB];
[lines addObject:line];
for(Line *l in lines) {
CGPoint pa = l.pointA;
CGPoint pb = l.pointB;
//NSLog(@"Point A: %@", NSStringFromCGPoint(pa));
//NSLog(@"Point B: %@", NSStringFromCGPoint(pb));
if ([self checkLineIntersection:pointA :pointB :pa :pb])
{
[pathArray removeLastObject];
[myPath removeAllPoints];
[self setNeedsDisplay];
NSLog(@"Removed path!");
return;
}
}
}
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(myPath.isEmpty) {
} else if ([lines count] != 0){
line = [[Line alloc]init];
line = [lines lastObject];
CGPoint pointA = line.pointA;
line = [[Line alloc]init];
line = [lines objectAtIndex:0];
CGPoint pointB = line.pointA;
[myPath closePath];
for(Line *l in lines) {
CGPoint pa = l.pointA;
CGPoint pb = l.pointB;
if ([self checkLineIntersection:pointA :pointB :pa :pb])
{
[pathArray removeLastObject];
[myPath removeAllPoints];
[self setNeedsDisplay];
NSLog(@"Removed path!");
return;
}
}
}
[self setNeedsDisplay];
}
-(BOOL)checkLineIntersection:(CGPoint)p1 :(CGPoint)p2 :(CGPoint)p3 :(CGPoint)p4
{
CGFloat denominator = (p4.y - p3.y) * (p2.x - p1.x) - (p4.x - p3.x) * (p2.y - p1.y);
/*
// In this case the lines are parallel so you assume they don't intersect
if (denominator == 0.0f)
return NO;
*/
CGFloat ua = ((p4.x - p3.x) * (p1.y - p3.y) - (p4.y - p3.y) * (p1.x - p3.x)) / denominator;
CGFloat ub = ((p2.x - p1.x) * (p1.y - p3.y) - (p2.y - p1.y) * (p1.x - p3.x)) / denominator;
if (ua > 0.0 && ua < 1.0 && ub > 0.0 && ub < 1.0)
{
return YES;
}
return NO;
}
@end
Line.h:
#import <UIKit/UIKit.h>
@interface Line : UIView
@property (nonatomic, assign) CGPoint pointA;
@property (nonatomic, assign) CGPoint pointB;
@end
Line.m:
#import "Line.h"
@implementation Line
@synthesize pointA;
@synthesize pointB;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
我希望有人也许能够回答这个问题。 很抱歉,如果这件事情很明显。 先感谢您!