簡體   English   中英

IOS中Button的動畫問題

[英]Animation issue of Button in IOS

我在主視圖控制器中有一個三個按鈕。 當我單擊 but1 時,它會執行動畫並向右移動,持續時間為特定寬度。 當 but1 達到其給定的寬度時,它會隱藏並在那里顯示 but2。 當我點擊but2時,它會執行動畫並移回but1的相同位置。 它就像一個按鈕打開和關閉的場景。 我現在面臨的問題是,當我打開 but1 時,它會移動其給定的寬度,當我關閉時,它會移回原來的 but1 位置,現在按鈕打開和關閉,但是當我們嘗試再次打開時,它不會用 but1 打開。 我希望每當用戶單擊 but1 時,它都應該隨着動畫向右移動,並且每次用戶想要關閉按鈕時,它都應該關閉它,而不是只打開或關閉一次。 我打開按鈕的代碼是這樣的,

- (IBAction)openHome:(id)sender {

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
CGAffineTransform transform = CGAffineTransformMakeTranslation(108, 0);
[_openHome setTransform:transform];
[UIView commitAnimations];
[NSTimer scheduledTimerWithTimeInterval:0.75
                                  target:self
                               selector:@selector(targetMethod:)
                               userInfo:nil
                                repeats:NO];

}

-(void)targetMethod:(NSTimer *)myTimer{
_home.hidden=NO;
_openHome.hidden=YES;
_closeHome.hidden=NO;

[myTimer invalidate];

}

關閉按鈕是這樣的,

- (IBAction)closeHome:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
CGAffineTransform transform = CGAffineTransformMakeTranslation(-110, 0);
[_closeHome setTransform:transform];
[UIView commitAnimations];
[NSTimer scheduledTimerWithTimeInterval:0.75
                                 target:self
                               selector:@selector(targetMethoded:)
                               userInfo:nil
                                repeats:NO];

}

-(void)targetMethoded:(NSTimer *)myTimer{
_home.hidden=YES;
_openHome.hidden=YES;
_closeHome.hidden=NO;

[myTimer invalidate];

}

按鈕的打開之前的畫面是這個, 在此處輸入圖片說明

按鈕打開后的畫面, 在此處輸入圖片說明

而不是使用 CGAffineTransform 更改位置,只需直接更改按鈕框架。 還可以使用以下代碼,它使用完成處理程序來調用 targetMethod 而不是使用 NSTimer:

[UIView animateWithDuration:0.75 animations:^{
    _openHome.frame = CGRectMake(_openHome.frame.origin.x+110, _openHome.frame.origin.y, _openHome.frame.size.width, _openHome.frame.size.height);
} completion:^(BOOL finished) {
    [self targetMethod];
}];

暫無
暫無

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

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