簡體   English   中英

如何停止正在移動的UIView?

[英]How to stop a UIView that is moving?

如何停止正在移動的UIView? 或正在減速的視圖。

問題是,例如:

我有一種方法,可以在按鈕的單擊事件后更改視圖的位置; 同時,另一個拖動事件可以更改視圖的位置。 如果先讓拖動事件發生,然后在View減速時,快速單擊B,則View的位置會根據B后面的方法發生變化,但它會立即返回到減速的位置並繼續減速。 視圖將不會返回到按鈕B表示減速的位置。

代碼如下。

首先,我使用KVO使View(即_topView是代碼)響應tableView的contentOffset(在拖動tableView時,View將移動)。

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

嘗試這段代碼將對您有幫助。

[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 ),所以它不是滾動視圖的最佳選擇。

據我了解問題,您應該停止tableview減速,然后調用button事件的方法。 在按下按鈕時停止減速:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM