Draw a vertical line on which the screen was deleted.

I want to draw a vertical line where the screen was used. Since the middle finger is more than 1 pixel wide, I want to do this "step by step". So basically, a line can only be displayed every 25 pixels. And I want to find out the nearest place where I could draw a line.

For example, if my finger displays 30 pixels on the left side of my top view, I want to draw a vertical line of 25 pixels on the left side of the view. If the screen displays 40 pixels on the left, I want the line to draw 50 pixels on the left side. (Thus, there can only be one line every 25 pixels, and I want to draw the closest.

Any idea how I could do this?

Line drawing is easy:

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(100.0, 0.0, 1, 320.0)]; lineView.backgroundColor = [UIColor whiteColor]; [parentView addSubview:lineView]; 

But I do not know how to find where the user tapped on the screen.

+4
source share
4 answers

To select the closest vertical line oriented to 25-point borders, use this to calculate the correct x value:

 CGFloat spacing = 25.0f; NSInteger lineNumber = (NSInteger)((touchPoint.x + (spacing / 2.0f)) / spacing); CGFloat snapX = spacing * lineNumber; 

Here is what happens in the code above:

  • Add half the distance value to the touch point - this is because the β€œsnap” process in the next step will always find the previous line, so by adding half the distance value, we will ensure that it β€œsnap” to the nearest line.
  • Calculate the line number by dividing by the interval and selecting the value as an integer. This reduces the fractional part of the result, so now we have the number of whole lines (0, 1, 2, 3, etc.).
  • Multiply the original distance to get the actual x value of the line you want to draw (0, 25, 50, 75, etc.).
+1
source

Create a transparent custom UIView that spans the entire screen (except the status bar) and overrides it (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event .
You can get the touch point with:

  UITouch *touch = [touches anyObject]; CGPoint touchPoint = [touch locationInView:self]; 
0
source

You can use ssteinberg code:

  UITouch *touch = [touches anyObject]; CGPoint touchPoint = [touch locationInView:self]; 

or adding a UITapGestureRecognizer to your view, which is probably simpler:

 UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; [yourParentView addGestureRecognizer:tapRecognizer]; [tapRecognizer release]; 

...

 - (IBAction)handleTap:(UITapGestureRecognizer *)recognizer { CGPoint touchPoint = [recognizer locationInView:self.view]; 

...

And besides, draw your line every 25px, adding something like this:

  CGFloat padding = 25.0; NSInteger xPosition = round(touchPoint.x / padding) * padding; [self drawLineAt:xPosition]; 
0
source

Refer to this if you find it appropriate:

 - (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]; } } 

}

0
source

Source: https://habr.com/ru/post/1412986/


All Articles