0

enter image description hereI have four ImageViews of equal heights and widths which fill up the screen in my iPhone Application and i need to drag and reposition all views using uipangesturerecogniser. I have added the necessary constraints to all the views such as equal heights for all and each views are connected to each other. Since each view connected to each other was a problem because if i drag one others also move. Hence, all views top are connected to top of the view and constant was changed when dragged as mentioned in link iOS >> Dragged View is Jumping Back to Original Position >> Auto Layout Combined with UIPanGestureRecognizer Issue. But when i drag one view and move another, the old views goes back to initial position. Hence, the problem is that i have tried many things and i have to maintain equal heights for all and should be able to drag and reposition all of them. Can some please help me with this issue?

Thank you

code:

func toppangesture(panGesture1:UIPanGestureRecognizer) 
{
 let loc=panGesture1.translation(in: self.view) 
var diff:Float 
if (panGesture1.state.rawValue==2)
 { 
let val=Float(loc.y)
 diff=Float(val) - Float((prevValue1?.y)!)
 let yval=CGFloat(diff)
 self.topimgyconstraint?.constant+=yval
 self.topimg.frame.origin.y+=yval
 self.view.layoutIfNeeded() 
prevValue1=loc;
 }
 else if (panGesture1.state.rawValue==1) 
{ self.prevValue1=loc 
}

}

Community
  • 1
  • 1
koushik v
  • 223
  • 6
  • 16
  • Actual code would help in this situation. – Sebastian Oct 11 '16 at 09:41
  • Another problem i noticed if all are not connected to each other and connected to top of the layout, the equal heights are not maintained for all views. All views overlap. I don't understand why equal heights constraint does not work – koushik v Oct 11 '16 at 09:42
  • The above lines of code are similar for all images. This is the pan gesture method. First, i get the began location and find the difference with previous locations and set it to the img y constraint constant and origin – koushik v Oct 11 '16 at 09:48
  • Can you edit the code into the question please? – Pekka Oct 11 '16 at 09:51
  • First thing - remove this: self.topimg.frame.origin.y+=yval. You shouldn't set the frame manually when using autolayout. – Sebastian Oct 11 '16 at 09:51
  • yeah that is how i did it in the beginning but it didn't work.Hence, i added it. – koushik v Oct 11 '16 at 09:55
  • this is just an opinion. i suggest you to achieve this task in a little different way. Use table view. add long press recognizer. on long press of any cell, hide it and create a snapshot of the cell and make it move with the changed state of pan gesture. detect drop location on gesture ended and put the cell after the destination and remove the hidden cell. i guess this way you can achieve in a better way. playing with constraint for rearranging requires changing of constraints already added. which i don't suggest. – Mahesh Agrawal Oct 11 '16 at 10:31
  • yeah i had that idea but i thought this is possible. If only somehow i can maintain the same height for all view without interconnection and drag the objects, it should be fine. – koushik v Oct 11 '16 at 10:34

0 Answers0