2012-02-24 35 views
1

我有一個帶有一些文本字段的scrollView。當鍵盤顯示時,滾動視圖會顯示。當鍵盤被隱藏時,滾動視圖會關閉。它工作正常。唯一的是鍵盤需要0.5秒才能出現,所以在那段時間裏我可以看到白色的背景。我想將我的scroll3的持續時間設置爲0.5。滾動視圖設置動畫持續時間

-(void)textFieldDidBeginEditing: (UITextField *)textField { 
NSLog(@"sowing keyboard"); 
scroll3.frame = CGRectMake(0, -200, 768, 960); 
[scroll3 scrollRectToVisible:scroll3.frame animated:YES]; 

} 


-(void)textFieldDidEndEditing: (UITextField *)textField{ 
NSLog(@"hiding keyboard"); 
scroll3.frame = CGRectMake(0, 44, 768, 960); 
} 

我該怎麼辦?我試過[scroll3 setAnimationDuration:0.5];但它不工作!請幫幫我!!!非常感謝。

回答

2

可能是這樣 -

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5]; 
    [scroll3 setFrame:CGRectMake(0, -200, 768, 960)]; 
    [UIView commitAnimations]; 
} 

-(void)textFieldDidEndEditing: (UITextField *)textField 
{ 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5]; 
    [scroll3 setFrame:CGRectMake(0, 44, 768, 960)]; 
    [UIView commitAnimations]; 
} 
+0

感謝,做工精細! – Alessandro 2012-02-24 15:30:58

1

由於UIScrollView管理自己的動畫,因此可以嘗試將decelerationRate設置爲更高的值,以便動畫需要更長的時間。我知道這適用於手動滑動,但我不確定這是否也適用於自動滑動(當您設置可視矩形時)。值得一試。

1

如果在您的情況下可能會使用NSTimer。 這樣你不會真的改變動畫的持續時間,但延遲一點點 ,以便在鍵盤顯示/隱藏時背景不可見。

這可能類似於下面的代碼:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2] target:self 
    selector:@SEL(scrollMyScrollView) userInfo:nil repeats:NO];  

這將0.2秒後調用給定的方法。 在「scrollMyScrollView」 - 方法你只是做你現在做的。

希望幫助;)