- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
NSLog("%f %f",currentPoint.x,currentPoint.y);
}
i want to developed a paint app for my ipad.
when i use these code and use finger to paint a line on my pad,
it's print
(x,1),(x,3),(x,6),(x,7),(x,12),(x,15),(x,18)....
in my thought,it should print
(x,1),(x,2),(x,3),(x,4),(x,5),(x,6),(x,7),(x,8),(x,9),(x,10),(x,11),(x,12),(x,13),(x,14),(x,15),(x,16),(x,17),(x,18)....
touchesMoved can not get continued coordinate ?
It depends on the speed that you swipe.
If you swipe really slow you'll probably get (x,1),(x,2),(x,3),(x,4),(x,5),(x,6),(x,7),(x,8),(x,9),(x,10), but if you swipe fast you can get as little as (x,1),(x,5),(x,10).
If you are developing a paint app you will have to take into account if the user hasn't lift his finger and paint the line between the points if he hasn't.
Good luck!
Related
I am using a UISwipeGestureRecogizer to swipe left and right by using UISwipeGestureRecognizerDirectionRight and UISwipeGestureRecognizerDirectionLeft and its working totally fine but the default swipe is too sensitive, its just a little bit more than a tap.
Is there any way by which I can make it work like when a scrollView works when its paging enable property is true that till the user doesnt lifts its finger up, its doesnt work.
It will be fine if the view doesn't move with the finger moving, and only work when a left or right gesture is performed and the finger is lifted. Thanks.
A UISwipeGestureRecognizer will not give you any option to change the sensitivity or any other aspect of the swipe. To obtain more control over the swipe, you could use one among the following:
UIPanGestureRecognizer
Here is something to try with UIPanGestureRecognizer:
- (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer{
CGPoint velocity = [gestureRecognizer velocityInView:yourView];
if(velocity.x > 0){
//Right gesture
}else{
//Left gesture
}
}
UIResponder
All UIViews inherit from UIResponder, so you can use the touchesBegan, touchesMoved and touchesEnded method for calculating the touch. Try something like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
start = [touch locationInView:self.view];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint end = [touch locationInView:self.view];
//Compare start and end points here.
}
I have to do some "snap to grid" behaviour,
I have a dragging image "sprite", but I need to "snap" this sprite to a certain button if touches go on top of that button,
so how can i know if touchesEnded is on top of a certain button,
here my code, but i dont know when touches where ended on top of butotn
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touched = [[event allTouches] anyObject];
CGPoint location = [touched locationInView:touched.view];
CGRect recta = touched.view.frame;
if (CGRectIntersectsRect(recta, self.button_1.frame)) {
DLog(#"intersected");
}
}
So this is not happening,
can it be done?
or do i have to check the X and Y position of finishing touch against the button frame X and Y position by my self?
cheers
touched.view is the view on which the touch started. You want to find out if the location where the touch ended is on top of a specific button. You can do this by checking if the location of the touch relative to the button is within the button's bounds.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touched = [[event allTouches] anyObject];
CGPoint locationRelative = [touched locationInView:self.button_1];
if (CGRectContainsPoint(self.button_1.bounds, locationRelative)) {
DLog(#"on top of button_1");
}
}
You can use CGRectIntersectsRect, which returns YES, if the two rectangles your pass to it intersect.
I use the following code to move an UIImageView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
paddle.center = CGPointMake(location.x, paddle.center.y);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesBegan:touches withEvent:event];
}
It works great, moving from left to right. But i can't figure out how to avoid moving over the screen, i meant to move it but don't intersect right and left edges of my screen. let's say 10 pixels from left and 10 from right.
First of all, you are the one saying this:
paddle.center = CGPointMake(location.x, paddle.center.y);
So where paddle is, is entirely up to you. If you don't want it at a certain x value, don't set it to that x value.
Second, never never call touchesBegan from within touchesMoved.
Third, you'd be much happier and safer using a UIPanGestureRecognizer. Situations like this are exactly what it's for.
I want to shift the position of some UILabels when the user swipes the screen--specifically, move them the same distance the user swiped their finger.
How would I go about detecting and implementing this?
Thanks
Override touchesBegan:withEvent and touchesMoved:withEvent:. Keep track of the starting location in touchesBegan:withEvent. If repeated calls to touchesDragged (it is called while the drag is happening) show that you are moving fast enough for the action to be a swipe versus just a drag, use the difference between the starting location and the current touch location to animate changing the UILabels.
You can do that by using following methods for UIView.
In touchesBegan you should store the position where the user first touches the screen. So you can identify the left or right swipe when touches ends.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
self.startPoint = [touch locationInView:self];
}
Record the final position on touchesEnded method. By comparing this two positions you can determine the direction of movement.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint endPosition = [touch locationInView:self];
if (startPoint.x < endPoint.x) {
// Right swipe
} else {
// Left swipe
}
}
I hope this helps.
Is there anyway to detect which pixels you are touching while keeping your hand/finger on the screen (iphone/ipad)? Essentially drawing a shape of my hand (not as detailed like a fingerprint).
Thanks.
What you want to achieve is sadly not possible. The current devices can only detect up to 11 touches as points (more info in this post). There is no way to get the real touch area or the true touched pixels.
If you are looking for the coordinate point of touch use following code.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.view];
NSLog(#"%f,%f",currentTouchPosition.x,currentTouchPosition.y);
}