繁体   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