如何停止正在移动的UIView?
如何停止正在移动的UIView? 或正在减速的视图。如何停止正在移动的UIView?
的问题是,例如:
我有一个方法变更后的按钮的点击事件视图的位置;同时另一个拖动事件可以改变视图的位置。如果我先让拖动事件发生,那么当视图减速时,快速点击B,视图的位置会根据B后面的方法改变,但它会立即返回到正在减速并继续减速的位置。该视图不会回到按钮B所指示的位置,即使它完成减速。
代码如下。
首先,我使用KVO使视图(即_topView是代码)响应tableView的contentOffset(在tableView被拖动时,视图将移动)。
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
CGPoint contentOffset = [change[NSKeyValueChangeNewKey] CGPointValue];
CGRect topFrame = _topView.frame;
topFrame.origin.y = -(contentOffset.y + TOP_SECTION_HEIGHT);
_topView.frame = topFrame;
}
然后在buttonB被点击时,下面的动画将被触发。
topFrame = CGRectMakke(...);
[UIView animateWithDuration:1 animations:^{
_topView.frame = topFrame;//topView
targetTableView.contentOffset = targetViewTargetOffset;
}];
正如我所理解的问题,你应该停止tableview减速,然后调用按钮事件的方法。 按下按钮时停止减速:
-(IBAction)buttonPressed:(id)sender
{
//this will stop table view animations
[targetTableView setContentOffset:targetTableView.contentOffset animated:NO];
//then you can adjust the frame of a view accurately
topFrame = CGRectMakke(...);
[UIView animateWithDuration:1 animations:^{
_topView.frame = topFrame;//topView
targetTableView.contentOffset = targetViewTargetOffset;
}];
}
谢谢,这解决了我的问题。顺便说一句,另一种方法是放弃KVO,因为它的代码不会执行tableView移动或减速。 – WhiteTherewasproblem
试试这个代码将有益的给你:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.1];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
// other animation properties
// set view properties
[UIView commitAnimations];
为什么你的观点是感动?要点是,如果它只是一个审美动画,您可以使用-snapshotViewAfterScreenUpdates:
创建视图的副本,隐藏您的原始视图并将此快照移到最重要的位置,然后删除快照并将原始视图移动到动画的结束位置。这是一个有点内存/电池昂贵,但你的动画是完全独立于你的KVO代码。
如果它是一个更重要的组件,在交互性更强的情况下,您应该为您的KVO添加一个标志(例如:isAnimated
)以防止动画启动时位置发生变化。
正如@Fogmeister所说,KVO不是动画的最佳选择,因为他们有一个代表(scrollviewDidScroll
),所以更多地使用滚动视图。
显示您的'UIView'“移动”代码。 – Raptor
KVO不是动画视图的好方法。为什么你不只是在动画块中一起制作动画? – SmokeDispenser
@JanGreve因为我必须使视图与tableView一起移动,即使后者正在减速 – WhiteTherewasproblem