我一直在寻找几个小时的答案,但找不到有关该主题的任何内容。
我有一个与 Objective-c 相关的问题。我正在制作一个应用程序,其中 UIView 检查用户的触摸,如果用户触摸并移动他/她的手指,则会绘制使用 UIBezierPath 的路径。如果用户绘制的路径与自身相交,则它应该从屏幕上消失。当用户绘制完图案后,如果这条线与另一条“线相交”,则路径中最后一点的线应自动与路径中的第一个点连接(我为此使用方法“closePath”) " 在路径中,路径也应该从屏幕上消失。
我将每个接触点存储在一个 CGPoint 中,该 CGPoint 存储在另一个名为 Line 的类中作为点 A 和点 B。然后我将“线”保存到名为“线”的 NSMutableArray 中。每次将一个点添加到路径时,我都会使用方法 (-(BOOL)checkLineIntersection:(CGPoint)p1 检查该点与在它与线中的任何“线”相交之前绘制的点之间的线是否相交:(CGPoint)p2 :(CGPoint)p3 :(CGPoint)p4) 我从本教程“http://www.iossourcecode.com/2012/08/02/how-to-make-a-game-like-割绳子第 2 部分/”。
问题
问题是,当我运行应用程序时,它有时会起作用,但有时当我绘制时,与路径相交的线不会消失。我不知道为什么......当我慢慢画时,它似乎更频繁地发生。
编码:
我的视图.h:
#import <UIKit/UIKit.h>
#import "Line.h"
@interface MyView : UIView {
NSMutableArray *pathArray;
UIBezierPath *myPath;
NSMutableArray *lines;
Line *line;
}
@end
我的视图.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
线.h:
#import <UIKit/UIKit.h>
@interface Line : UIView
@property (nonatomic, assign) CGPoint pointA;
@property (nonatomic, assign) CGPoint pointB;
@end
线米:
#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
我希望有人能够回答这个问题。对不起,如果这是显而易见的。先感谢您!