I have a pan gesture recogniser acting as a slider, code as follows:
- (void)minutePan:(UIPanGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)){
CGPoint translation = [gesture translationInView:gesture.view.superview];
float leftBound = gesture.view.bounds.size.width / 2.0f;
float rightBound = gesture.view.bounds.size.width + (leftBound - 60);
float position;
if(gesture.view.center.x < leftBound){
position = leftBound;
}else if(gesture.view.center.x > rightBound){
position = rightBound;
}else{
position = gesture.view.center.x + translation.x;
}
gesture.view.center = CGPointMake(position, gesture.view.center.y);
[gesture setTranslation:CGPointZero inView:gesture.view.superview];
}
}
I have a UIView with the gesture recogniser attached inside another UIView acting as the boundary for it. Its working fine except when I get to the left and right edges. The position variable should not get set lower than half the panned views width but it seems to be updating the view on the screen before the code runs. This causes the view to go outside the bounds and flicker back as long as the user is dragging. Any help would be greatly appreciated thanks.
Related
I have looked at this post and rickster's answer and lorenzo's sample code
Lorenzo's code works well. I am trying to extend it and add a conventional pan where you can move the scene left, right, up and down.
Here is what I have tried:
-(void) handlePan2F :(UIPanGestureRecognizer*)recognizer {
CGPoint translation = [recognizer translationInView:recognizer.view];
CGFloat xVal = translation.x * 0.025 ; // apply some damping
CGFloat yVal = translation.y * 0.025 ;
if (recognizer.state == UIGestureRecognizerStateChanged) {
self.cameraOrbit.position = SCNVector3Make(-xVal, yVal ,0.0); // what should the value be for z?
}
}
The issues I am running into are:
The pan works kind of - but not well.
If I add a SCNLookAtConstraint to point an area in the scene then the
pan2F gesture rotates the scene. Why is that?
The behavior I am trying to implement is to just to pan left/right/up down over the scene without any scene rotation. How to fix this code?
I am willing to give up SCNLookAtConstraint as long as I can zoom to a specific area within the scene.
In my UITableViewCell subclass I add a pan gesture and in gestureRecognizerShouldBegin method I checked self.frame.origin.x and self.frame.origin.y both are 0.000000 and 0.000000and after applying TranslationInView CGPoint translation = [gestureRecognizer translationInView:[self superview]]; I am getting x=-4.000000 and y=0.000000
How TranslationInView work, I am trying to wrap my head around it, when I am getting the correct location of cell 0.0 and 0.0 because the first cell will have 0.0 and 0.0, why I need TranslationInView.
TranslationInView is a method of UIPanGestureRecognizer and it tells you how far the touch moved since it was last reset. It resets when the touch goes down or if you reset it yourself.
For example
- (void) pan: (UIPanGestureRecognizer *) recognizer
{
if ((recognizer.state == UIGestureRecognizerStateChanged)||(recognizer.state == UIGestureRecognizerStateEnded)) {
CGPoint translation = [recognizer translationInView:self];
}
}
The CGPoint traslation gets increased/decreased the distance that the gesture has moved.
So I'm trying to make an app where you pick up objects with your finger, it moves with your finger and then you flick it away to throw it. I found some threads for similar games but not with this particular problem.
Since I couldn't really keep the object along with my finger by applying force or impulse I disable the physics on it while I'm "holding it", measure velocity so when I release it I apply that vector as an impulse.
This works excellent visually but when I try to read the position of the object it's not synced up with "reality". I've made a condition so if velocity isn't strong enough when you release the object it is supposed to return to its starting position but it doesn't move at all (except if you manage to throw it, that works fine) so when I tried to output the .position to the console it didn't correlate to what I was seeing on the screen.
Any ideas? I know you shouldn't set position and simulate physics at the same time but I disable the physics for when I'm dragging the object. Why won't it keep track of .position just because it is moving it with physics?
Here I try to throw a potion:
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) { //Picked up
potion.physicsBody.dynamic = NO;
potion.physicsBody.velocity = CGVectorMake(0.0, 0.0);
potion.physicsBody.angularVelocity = 0.0;
potion.zRotation = 0.0;
CGPoint touchLocation = [recognizer locationInView:recognizer.view];
touchLocation = [self convertPointFromView:touchLocation];
[self selectNodeForTouch:touchLocation];
} else if (recognizer.state == UIGestureRecognizerStateChanged) { //Moved around
CGPoint translation = [recognizer translationInView:recognizer.view];
translation = CGPointMake(translation.x, -translation.y);
[self panForTranslation:translation];
[recognizer setTranslation:CGPointZero inView:recognizer.view];
velocity = CGVectorMake(translation.x, translation.y);
} else if (recognizer.state == UIGestureRecognizerStateEnded) { //Dropped
_selectedNode = nil;
potion.physicsBody.dynamic = YES;
if (potion.position.y < 250 && velocity.dy < 20) { //Returned to starting position
NSLog(#"Pos:%f,%f",potion.position.x,potion.position.y);
potion.physicsBody.velocity = CGVectorMake(0.0, 0.0);
potion.physicsBody.angularVelocity = 0.0;
potion.zRotation = 0.0;
potion.position = CGPointMake(gameWidth / 2, 150);
NSLog(#"Returning potion");
NSLog(#"Pos:%f,%f",potion.position.x,potion.position.y);
} else { //Flung away
[potion.physicsBody applyImpulse:velocity];
NSLog(#"Add impulse: %f, %f", velocity.dx, velocity.dy);
NSLog(#"Pos:%f,%f",potion.position.x,potion.position.y);
}
}
}
EDIT:After some more testing I've observed that if it has never been thrown (dynamic with impulse) it always shows (160, 150) as its position no matter where it is. Sometimes it shows something like 160.00031 and I don't know why. After it has been thrown it shows the correct position in the NSLog but changing the position still doesn't work
EDIT2: Just saw that it doesn't show the correct position after a throw. Just different but they seem offset. I throw objects in the +y direction (up) and when I put it down in the left corner the position.x is correct but position.y is offset by about 300. So where y should be 0 it's 300.
I've used the following code, shown below, to implement drag & drop in several different places and has always worked well for me in the past.
Now I have a problem with it and have no idea why. It works perfectly so long as I have the device (or simulator) oriented portrait with the button down. But in any of the other three orientations, as the finger dragging the view moves one direction, the "dragged" view moves a different direction.
As shown below I'm logging the value of translation with each move. In the original orientation, as I drag from the middle of the screen toward the lower-left corner, the values for translation are:
-, +
If I rotate left, and do it again:
-, -
Rotate left again:
+, -
Rotate left again:
+, +
I am totally not getting what happens here, particularly since this code seems to work well in other view controllers.
Any suggestions will be appreciated.
- (void) didMakePanGesture:(UIPanGestureRecognizer *)panGesture
{
if (panGesture.state == UIGestureRecognizerStateBegan)
{
[self setDropTargetsCoordinates]; // saves correct drop target & its coordinates
dragViewStartLocation = receptiveClassificationImageView.center; // save center in case we have to snap back
receptiveClassificationImageView.transform = CGAffineTransformMakeScale(0.40f, 0.40f); // make the image smaller for dragging
receptiveClassificationImageView.layer.cornerRadius = 12.0f; // we lose the rounded corners in the scaling; this to fix
}
else if (panGesture.state == UIGestureRecognizerStateChanged)
{
//
// Adjust the location of the dragged view whenever state changes
//
CGPoint translation = [panGesture translationInView:nil];
CGAffineTransform transform = receptiveClassificationImageView.transform;
transform.tx = translation.x;
transform.ty = translation.y;
receptiveClassificationImageView.transform = transform;
NSLog(#"Translation=%f,%f" translation.x, translation.y);
}
else if (panGesture.state == UIGestureRecognizerStateEnded)
{
// do stuff when dropped
}
Just in case someone sees this later, this problem was solved with the following changes to the code above:
if (panGesture.state == UIGestureRecognizerStateBegan)
{
[self setDropTargetsCoordinates]; // saves correct drop target & its coordinates
dragViewStartLocation = receptiveClassificationImageView.center; // save center in case we have to snap back
**receptiveClassificationImageView.center = [panGesture locationInView:receptiveClassificationImageView.superview]; // re-center the view before scaling**
receptiveClassificationImageView.transform = CGAffineTransformMakeScale(0.40f, 0.40f); // make the image smaller for dragging
receptiveClassificationImageView.layer.cornerRadius = 12.0f; // we lose the rounded corners in the scaling; this to fix
}
else if (panGesture.state == UIGestureRecognizerStateChanged)
{
//
// Adjust the location of the dragged view whenever state changes
//
CGPoint translation = [panGesture translationInView:**self.view**];
CGAffineTransform transform = receptiveClassificationImageView.transform;
transform.tx = translation.x;
transform.ty = translation.y;
receptiveClassificationImageView.transform = transform;
}
I am building an iPad app where I needed to allow resizing views functionality using divider view provided between two views. The divider view is just a 20px height view between two half screen content views - please refer attached images.
When user scrolls this divider view up or down, both content views changes their sizes appropriately. I have extended UIView and implemented this using touchMoved delegate as code given below in touchesMoved delegate. It works fine. The only thing is missing with TouchMoved is you can't flick divider view to top or bottom directly. You have to drag all the way to top or bottom!
To support flicking the view I have tried UIPanGestureRecognizer but I don't see smooth dragging with it. Setting split-position of parent resizes both content views as shown in the code. When I handle split position change in UIGestureRecognizerStateChanged state, just touching divider view flick it to top or bottom. Handling split position change in UIGestureRecognizerStateEnded does the same but I don't see content view resizing with dividerview scrolling!
Could someone please tell me how could I achieve both smooth scrolling of divider view with resizing content views (like touchMoved) and flicking the view? Any alternative approach would also be fine. Thanks.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch) {
CGPoint lastPt = [touch previousLocationInView:self];
CGPoint pt = [touch locationInView:self];
float offset = pt.y - lastPt.y;
self.parentViewController.splitPosition = self.parentViewController.splitPosition + offset;
}
}
- (void)handlePan:(UIPanGestureRecognizer*)recognizer {
CGPoint translation = [recognizer translationInView:recognizer.view];
CGPoint velocity = [recognizer velocityInView:recognizer.view];
if (recognizer.state == UIGestureRecognizerStateBegan) {
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
// If I change split position here, I don't see smooth scrolling dividerview...it directly jumps to the top or bottom!
self.parentViewController.splitPosition = self.parentViewController.splitPosition + translation.y;
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
// If I change split position here, the same thing happens at end and I don't see my divider view moving with my scrolling and resizing my views.
self.parentViewController.splitPosition = self.parentViewController.splitPosition + translation.y;
}
}
Initial screen
Increased top view size by scrolling divider view towards bottom.
Top view is totally hidden here but I have to scroll divider view all
the way to top. I want to flick the divider view so that it directly
goes from any position to top
If I understand you correctly, you have two separate problems here.
1st: The 'unsmooth' dragging when using the GestureRecognizer. The problem in your code is, that you add the translation every time the GR calls it's handle method. Since the translation is measured continuously, you're adding a higher value every time it's called (i.e. the first call adds 10 to the split position, the 2nd 20, the 3rd 30 and so on).
To solve this you should set the translation to zero after you've updated the split position:
- (void)handlePan:(UIPanGestureRecognizer*)recognizer {
CGPoint translation = [recognizer translationInView:recognizer.view];
if (recognizer.state == UIGestureRecognizerStateChanged) {
self.parentViewController.splitPosition = self.parentViewController.splitPosition + translation.y;
}
[recognizer setTranslation:CGPointZero inView:recognizer.view];
}
2nd: To allow the user to flick the split position to the top or bottom, you could check the velocity in UIGestureRecognizerStateEnded, and if it exeeds some value instantly set the splitposition to top or bottom.
But it might be more convenient to use a UISwipeGestureRecognizer.
- (void)handleSwipe:(UISwipeGestureRecognizer *)gr
{
if (gr.direction == UISwipeGestureRecognizerDirectionUp){
[self.parentViewController moveSplitViewToTop];
}
else if (gr.direction == UISwipeGestureRecognizerDirectionDown){
[self.parentViewController moveSplitViewToBottom];
}
}
In this case it might be necessary (not sure, maybe it's not) to require the SwipeGR to fail before the PanGR triggers by setting:
[panGestureRecognizer requireGestureRecognizerToFail:swipeGestureRecognizer];
Hope that helped.
Edit:
Regarding your comment, I assume by frequency you mean velocity. If you only use the PanGR you could modify the code above to sth. like this:
- (void)handlePan:(UIPanGestureRecognizer*)recognizer {
CGPoint translation = [recognizer translationInView:recognizer.view];
CGPoint velocity = [recognizer velocityInView:recognizer.view];
if (recognizer.state == UIGestureRecognizerStateChanged) {
self.parentViewController.splitPosition = self.parentViewController.splitPosition + translation.y;
}
else if (recognizer.state == UIGestureRecognizerStateEnded){
if (velocity.y > someValue){
[self.parentViewController moveSplitViewToBottom];
}
else if (velocity.y < someValue){
[self.parentViewController moveSplitViewToTop];
}
}
[recognizer setTranslation:CGPointZero inView:recognizer.view];
}
I'm not sure if the velocity is signed, if not you've got to check for the translation again in the if-block.