iOS Dragged View is Jumping Back to Original Position Auto Layout Combined with UIPanGestureRecognizer Issue
I think that if you are using autolayout and you want to change the position of a view, you need to be updating the constraints on the view instead of manually setting the center of a view. From the description it sounds like you are setting the center, but when the app next lays out the views (eg you drag another) it re-sets the center to whatever autolayout says it should be.
I'm not sure what the rest of your app looks like, but you may be able to find nearby views and add constraints to it, or you might prefer to just update the constraints of the dragged view and set the leading space and top space to the superview manually. If you've got a collection handy you could also clear all the constraints and then re-add them as you'd like to keep padding in between the views.
To set the constraints in code you can clear the constraints for the moved view and re-add them, or if you have a view in IB you can drag the constraints into code to create an outlet. Look at the removeConstraints and addConstraints methods to do it in code.
Lastly, if it works how you'd like without autolayout, you could try setting translatesAutoresizingMaskIntoConstraints = YES on the moved views, but be aware that may cause conflicts elsewhere.
bringSubviewToFront moves UIView back to original location
You've put your finger on the matter. Think of it this way:
Auto Layout positions views.
You are positioning a view (in code).
Well, those two things are in conflict. You must not do what you are doing.
If you want to move a view in code, when that view is also under the influence of Auto Layout, you must change the constraints that position it, so that Auto Layout positions it where you want it. If you are going to use Auto Layout on a view, then in repositioning it, you must work with Auto Layout, not against it.
view slide (follow finger position vertically up/down) with uipangesturerecognizer and auto layout
after some searching and reading I found out what I was doing wrong. Here is how I solved it, just in case somebody else wanna know it.
-(void)(UIPanGestureRecognizer *) pan
{
CGPoint velocityOfPan = [pan velocityInView:self.view];
CGFloat viewHeight = pan.view.superview.height;
CGPoint delta;
switch(pan.state)
{
case UIGestureRecognizerStateBegan:
self.startLocation = delta = [pan translationInView:self.view];
break;
case UIGestureRecognizerStateChanged:
{
delta = CGPointApplyAffineTransform([pan translationInView:self.view], CGAffineTransformMakeTranslation(-self.startLocation.x, -self.startLocation.y));
self.startLocation = [pan translationInView:self.view];
if(delta.y < 0 && self.verticalConstraint.constant < 0)
{
delta.y = 0;
}
else
{
delta.y = self.verticalConstraint.constant + delta.y;
}
self.verticalConstraint.constant = delta.y;
[self updateViewAnimation];
break;
}
case UIGestureRecognizerStateEnded:
{
if (velocityOfPan.y > 0)
{
delta.y = (viewHeight - MIN_SIZE);
}
else if(velocityOfPan.y < 0)
{
delta.y = 0;
}
self.verticalConstraint.constant = delta.y;
[self updateViewAnimation];
break;
}
default:
break;
}
}
View on screen Resizing and Repositioning itself when new View is added into hierarchy
If you are using autoLayout
, you must add constraints for all the views in the scene.
Since you are not adding any constraints for the view A
, the system will add constraints internally to place the view at that specific position. If you then move the view manually using frames, this will not hold as the constraints will force the view to move to its original position.
You can approach this problem in two ways:
- Manually add constraints to all views in the scene, whether it is in storyboard or added programatically. Each constraint has a
constant
attribute. Change the value of thisconstant
if you need to resize or reposition the view. - Disable autolayout altogether from the scene. You may then reposition or resize views by changing frame.
Hope this helps! :)
Related Topics
iOS 11 Core Nfc - Any Sample Code
Saving Coredata To-Many Relationships in Swift
Ios: Perform Upload Task While App Is in Background
Error: Module File's Minimum Deployment Target Is iOS8.3 V8.3
Programmatically Get Path to Application Support Folder
Xcode Is Creating Generic Xcode Archive Instead of iOS App Archive
How to Rotate an Uiimageview by 20 Degrees
Nsurlsession "Http Load Failed Kcfstreamerrordomainssl, -9813 ; Self Signing Certificate
How Does One Print All Wkwebview on and Offscreen Content Osx and iOS
Using Custom Fonts in Interface Builder
iOS Provisioning and Certifcates - Will Revoke/Renew Effect App Store Apps
Expanding and Collapsing Table View Cells in iOS
How to Check If iOS App Is in Background
Uiview Atop the Keyboard Similar to Imessage App