滑动uiview保持动画

时间:2012-07-10 03:04:29

标签: ios animation uiview cgrect

我想我会从代码开始......

- (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];
}

我有一张地图,用户可以在其中点按商家,并且uiview“directionsView”会从底部向上滑动。点击多个业务时会出现此问题。视图持续攀升49像素。我如何防止这种情况发生?

我有另一种方法来定义取消选择业务时会发生什么,我尝试使用相同的动画方法,只是反向(使用setHidden:YES),但没有运气:)

请帮忙吗?

1 个答案:

答案 0 :(得分:0)

每次调用此方法时,都会从self.directionView的原点的Y分量中减去:

self.directionsView.frame = CGRectOffset(self.directionsView.frame, 0, -self.directionsView.frame.size.height);

因此,如果您多次点击而没有重置视图的位置,它将继续在其父视图中向上滑动(可能在屏幕顶部,我不小心这样做时总是觉得很有趣)。

最简单的解决方案是定义两个CGRect,并根据您是想要在屏幕上还是在屏幕外直接将一个或另一个分配给self.directionView.frame。您可以连续多次调用这些效果,效果不会像示例中那样累积。

CGRect onScreen = CGRectMake(x, y, l, w); //Fill in actual values
CGRect offScreen = CGRectMake(x, larger_value_of_y, l, w); //Again, use actual values

您还可以将帧设置为其正常的“屏幕上”值,并调整directionView的transform属性,该属性也是可动画的。同样,您可以多次应用其中任何一种,并且效果不会累积。

//on screen
self.directionsView.transform = CGAffineTransformIdentity; //"identity" meaning no change to position
//off screen
self.directionsView.transform = CGAffineTransformMakeTranslation(0, self.directionsView.bounds.size.height); //Shifts 0 pts right, and height pts down

注意在上面的代码中使用“bounds”。当您更改变换时,框架将变为未定义,因此如果您尝试设置框架(或基于当前框架的任何其他计算),当变换不是标识时,视图可能会无法预测地移动。

(免责声明:代码从内存中输入 - 未在XCode中测试。)