簡體   English   中英

iOS-按鈕從右側動畫滑入

[英]IOS - buttons slide in from right side animation

我是IOS編程的新手。 我的屏幕上有背景圖片。 我想要的是當應用啟動時屏幕上顯示背景圖像,然后1秒鍾后,屏幕右側出現2個按鈕。 我怎樣才能做到這一點。 這是我正在嘗試做的圖像

在此處輸入圖片說明

按鈕1和button2必須先不可見。 1秒后,它們從右側出現。 如果有任何與這種動畫有關的教程,請分享

您可以使用簡單的UIView動畫來實現此目的。

將您的按鈕x原點設置為大於屏幕大小的任何值,以使其首先不可見。 然后使用視圖動畫將按鈕框重置為可見值。 例如,在您的viewWillAppear:方法中調用以下函數。

- (void) animateButton {

  // Setting button origin to value greater than the view width.
  CGRect frame = button.frame;
  frame.origin.x = self.view.frame.size.width+10;// or set any value > 320
  button.frame = frame;

    [UIView animateWithDuration:0.5
                          delay:2.0
                        options: UIViewAnimationCurveEaseOut
                     animations:^{
                         button.frame = CGRectMake(20, 100, 60, 25) ;// Any value according to your requirement
                     } 
                     completion:^(BOOL finished){
                         NSLog(@"Done!");
                     }];
}

參考教程

設置按鈕的x坐標,以使按鈕位於屏幕的右側。

然后在您的viewDidLoad()嘗試以下操作

[UIView animateWithDuration:0.5
                              delay:1.0
                            options:NO
                         animations:^{ change your button x co- ordinate here to the co-ordinate you need it on finally}
                         completion:NULL];

您可以通過這種方式進行操作,請注意,如果要對按鈕的顯示方式進行動畫處理,則必須使用alpha = 0.0f (而不是hidden = true )將其hidden = true 這樣,您將可以流暢地顯示動畫!

這里是 :

首先,在UIViewControllerviewDidLoad上,必須將NSTimer設置為1sec,然后讓它調用將使按鈕出現的方法:

- (void) viewDidLoad {
    [super viewDidLoad];
    // Do your init stuff here

    // We will call your buttons as if they are declared as properties of your class, ask if you don't know how to set them
    self.button1.alpha = 0.0f;
    self.button2.alpha = 0.0f;

    // This will wait 1 sec until calling showButtons, where we will do the trick
     NSTimeInterval timeInterval = 1.0;
    [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(showButtons) userInfo:nil repeats:NO];
 }

- (void) showButtons {
    [UIView beginAnimations:@"buttonShowUp" context:nil];
    [UIView setAnimationDuration:0.5]; // Set it as you want, it's the length of your animation

    self.button1.alpha = 1.0f;
    self.button2.alpha = 1.0f;

    // If you want to move them from right to left, here is how we gonna do it :
    float move = 100.0f; // Set it the way you want it
    self.button1.frame = CGRectMake(self.button1.frame.origin.x - move,
                                    self.button1.frame.origin.y,
                                    self.button1.frame.size.width,
                                    self.button1.frame.size.height);

    self.button2.frame = CGRectMake(self.button2.frame.origin.x - move,
                                    self.button2.frame.origin.y,
                                    self.button2.frame.size.width,
                                    self.button2.frame.size.height);

     // If you want to set them in the exact center of your view, you can replace 
     // self.button1.frame.origin.x - move
     // by the following
     // (self.view.center - self.button1.frame.size.width/2)
     // This way, your button will be centered horizontally

    [UIView commitAnimations];
}

詢問您是否有任何疑問!

暫無
暫無

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

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