I'm facing a very strange problem - at least to me - and it seems like I'm going crazy :) I've got a UIView to which I add a UIPanGestureRecognizer in order to be able to move it to some extent vertically on the screen; here's the code where I do it:
- (id)init {
// ...
UIPanGestureRecognizer *gestureRecognizerPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesturePan:)];
[self.viewMenuProper addGestureRecognizer:gestureRecognizerPan];
imageArrowOriginalFrame = self.imageArrow.frame;
// ...
}
Said UIView, self.viewMenuProper
, is supposed to be draggable towards the bottom of the screen (and back up towards it default position). Just above it on the screen is placed a UIImageView whose top edge's position and width are meant to maintain their values but its height is supposed to be changed depending on the position of the self.viewMenuProper
so that it seems like it connects that UIView with some point at the top of the screen even when the UIView is moved around.
So here's the method which handles the gestures:
- (void)handleGesturePan:(UIPanGestureRecognizer *)gestureRecognizerPan {
if (gestureRecognizerPan.state == UIGestureRecognizerStateBegan) {
gesturePanPreviousY = [gestureRecognizerPan locationInView:self].y;
}
else if (gestureRecognizerPan.state == UIGestureRecognizerStateChanged) {
float gesturePanCurrentY = [gestureRecognizerPan locationInView:self].y;
float gesturePanDeltaY = gesturePanCurrentY - gesturePanPreviousY;
float imageArrowFrameNewHeight = self.imageArrow.frame.size.height + gesturePanDeltaY;
if (imageArrowFrameNewHeight <= 200 && imageArrowFrameNewHeight >= imageArrowOriginalFrame.size.height) {
[self.imageArrow setFrame:CGRectMake(imageArrowOriginalFrame.origin.x, imageArrowOriginalFrame.origin.y, imageArrowOriginalFrame.size.width, imageArrowFrameNewHeight)]; // line A
}
// Check if the transformed View won't be too far up any way of the screen
float gesturePanNewY = self.viewMenuProper.transform.ty + gesturePanDeltaY;
if (gesturePanNewY <= 200 && gesturePanNewY > 0) {
[self.viewMenuProper setTransform:CGAffineTransformTranslate(self.viewMenuProper.transform, 0, gesturePanDeltaY)]; // line B
}
gesturePanPreviousY = gesturePanCurrentY;
}
// The below animates the Views back to their default positions
else if (gestureRecognizerPan.state == UIGestureRecognizerStateEnded || gestureRecognizerPan.state == UIGestureRecognizerStateFailed || gestureRecognizerPan.state == UIGestureRecognizerStateCancelled) {
[UIView animateWithDuration:1.0 animations:^{
[self.viewMenuProper setTransform:CGAffineTransformIdentity];
// NOTE: the below line prevents the animation from jumping wrong way 1st before animating the way it should
// http://stackoverflow.com/questions/12535647/uiview-animation-jumps-at-beginning
[self layoutIfNeeded];
}];
self.imageArrow.frame = imageArrowOriginalFrame;
}
}
Now this doesn't work: what happens is viewMenuProper
moves nicely but imageArrow
only stretches when the former one is at its furthermost position and not moving anymore. If I comment out line A (take a look at the code above), then line B works fine; similarly, if I comment out line B, then line A works fine. But when they're both uncommented, it's just like I wrote: imageArrow
doesn't stretch like expected.
Any ideas? If you need any more clarification let me know.
Edit: both the UIView
and the UIImageView
are children of the same parent UIView
- they're not "connected" to one another in the .xib file where I created their layout.