I suppose I’ll start with the code…
- (void) mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
DLog(@"annotation: %@", [[view annotation] title]);
self.selectedAnnotation = [view annotation];
[self.directionsView setHidden:NO];
[UIView beginAnimations:@"slideup" context:NULL];
self.directionsView.frame = CGRectOffset(self.directionsView.frame, 0, -self.directionsView.frame.size.height);
[UIView commitAnimations];
}
I have a map where users can tap on a business and a uiview “directionsView” slides up from the bottom. The problem occurs when more than one business is tapped. The view keeps climbing by 49 pixels. How do I keep this from happening?
I have another method that defines what happens when a business is deselected and I tried using the same animation method, only in reverse (with setHidden:YES), but no luck 🙂
Help pretty please?
Every time this method gets invoked, you’re subtracting from the Y component of the self.directionView’s origin:
So if you tap multiple times without something else resetting the view’s position, it will keep sliding up in its parent view (perhaps off the top of the screen, which I always find entertaining when I accidentally do this).
The simplest solution is to define two CGRects, and directly assign one or the other to self.directionView.frame based on whether you want the view on- or off-screen. You can call these multiple times in a row, and the effect won’t be cumulative like in your example.
You could also set the frame to its normal “on-screen” value, and adjust the directionView’s transform property, which is also animatable. Again, you can apply either of these multiple times and the effects aren’t cumulative.
Note the use of “bounds” in the above code. The frame becomes undefined when you change the transform, so the view may move unpredictably if you try to set the frame (or base any other calculations on the current frame) when the transform is anything other than identity.
(Disclaimer: code typed from memory–not tested in XCode.)