简体   繁体   English

滑动uiview保持动画

[英]Sliding uiview keeps animating

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. 我有一张地图,用户可以在其中点击业务,而uiview“ directionsView”则从底部向上滑动。 The problem occurs when more than one business is tapped. 当利用多个业务时,会发生此问题。 The view keeps climbing by 49 pixels. 该视图保持爬升49像素。 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 :) 我有另一种方法来定义取消选择业务时会发生的情况,并且我尝试使用相同的动画方法,只是相反(使用setHidden:YES),但是没有运气:)

Help pretty please? 请帮忙吗?

Every time this method gets invoked, you're subtracting from the Y component of the self.directionView's origin: 每次调用此方法时,您都要从self.directionView的原点的Y分量中减去:

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

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. 最简单的解决方案是定义两个CGRect,然后根据要在屏幕上还是在屏幕外查看将一个或另一个直接分配给self.directionView.frame。 You can call these multiple times in a row, and the effect won't be cumulative like in your example. 您可以连续调用这些多次,并且效果不会像您的示例那样累积。

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

You could also set the frame to its normal "on-screen" value, and adjust the directionView's transform property, which is also animatable. 您也可以将框架设置为其正常的“屏幕上”值,并调整directionView的transform属性,该属性也可以设置动画。 Again, you can apply either of these multiple times and the effects aren't cumulative. 同样,您可以多次应用这两种方法中的任何一种,并且效果不是累积性的。

//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

Note the use of "bounds" in the above code. 注意上面代码中“ bounds”的使用。 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.) (免责声明:从内存键入的代码-未在XCode中进行测试。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM