Ios 在屏幕被轻触的位置画一条垂直线
我想在屏幕被点击的地方画一条垂直线。因为手指的平均宽度超过1像素宽,所以我想“分步”进行。所以基本上,这条线只能每25像素画一次。我想找出最近的位置,在那里我可以画一条线 例如,如果手指从上视图左侧点击30个像素,我想从视图左侧绘制一条25个像素的垂直线。如果屏幕从左边点击40个像素,我想从左边画50个像素的线。(因此每25个像素只能有一条线,我想画一条最近的线 你知道我该怎么做吗 划清界线很容易:Ios 在屏幕被轻触的位置画一条垂直线,ios,uiview,tap,Ios,Uiview,Tap,我想在屏幕被点击的地方画一条垂直线。因为手指的平均宽度超过1像素宽,所以我想“分步”进行。所以基本上,这条线只能每25像素画一次。我想找出最近的位置,在那里我可以画一条线 例如,如果手指从上视图左侧点击30个像素,我想从视图左侧绘制一条25个像素的垂直线。如果屏幕从左边点击40个像素,我想从左边画50个像素的线。(因此每25个像素只能有一条线,我想画一条最近的线 你知道我该怎么做吗 划清界线很容易: UIView *lineView = [[UIView alloc] initWithFrame
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(100.0, 0.0, 1, 320.0)];
lineView.backgroundColor = [UIColor whiteColor];
[parentView addSubview:lineView];
但我不知道如何找到用户点击屏幕的位置。创建一个透明的自定义UIView,覆盖整个屏幕(状态栏除外),并覆盖其
(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
您可以通过以下方式获得接触点:
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
您可以使用ssteinberg代码:
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
或者在视图中添加UITapGestureRecognitor,这可能更容易:
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[yourParentView addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
另外,在每25像素处画一条线,添加如下内容:
CGFloat padding = 25.0;
NSInteger xPosition = round(touchPoint.x / padding) * padding;
[self drawLineAt:xPosition];
要选择与25点边界对齐的最近垂直线,请使用此选项计算正确的x值:
CGFloat spacing = 25.0f;
NSInteger lineNumber = (NSInteger)((touchPoint.x + (spacing / 2.0f)) / spacing);
CGFloat snapX = spacing * lineNumber;
下面是上面代码中发生的情况:
如果您觉得合适,请参考以下内容:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}你想画一条线还是多条线……比如说,开头没有线,他以100像素的速度点击,所以你想要四条线?只有一条线,如果有两次点击,我会移动这条线。请记住屏幕宽度(或高度)不是25的倍数。我会使用10+25n,因为这是对称的。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
if (lineView) {
[lineView removeFromSuperview];
NSSet *allTouchesSet = [event allTouches];
NSArray *allTouches = [NSArray arrayWithArray:[allTouchesSet allObjects]];
int count = [allTouches count];
if (count == 1) {
UITouch *touch = [[event allTouches] anyObject];
tapPoint = [touch locationInView:self];
NSLog(@"tapPoint: %@",NSStringFromCGPoint(tapPoint));
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(tapPoint.x, 0.0, 1, 320.0)];
lineView.backgroundColor = [UIColor whiteColor];
[parentView addSubview:lineView];
}
}