IOS - 按钮从右侧动画滑入

Posted

技术标签:

【中文标题】IOS - 按钮从右侧动画滑入【英文标题】:IOS - buttons slide in from right side animation 【发布时间】:2014-04-03 09:22:49 【问题描述】:

我是 ios 编程新手。我的屏幕上有一张背景图片。我想要的是当应用程序启动屏幕时显示背景图像,然后在 1 秒后 2 个按钮从屏幕右侧出现。我怎样才能做到这一点。这是我想要做的图像

按钮 1 和按钮 2 必须首先不可见。 1 秒后,它们从右侧升起。 如果有这种动画相关的教程,请分享

【问题讨论】:

***.com/questions/11723545/… 看这里。这应该适合你。 【参考方案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!");
                     ];

参考Tutorial

【讨论】:

【参考方案2】:

您可以这样做,请注意,如果您想以动画方式显示按钮,您必须使用alpha = 0.0f(而不是hidden = true)隐藏它们。这样,您将获得流畅的显示动画!

这里是:

首先,在您的UIViewControllerviewDidLoad 上,您必须设置一个1 秒的NSTimer,然后让它调用让您的按钮出现的方法:

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

有什么问题可以问!

【讨论】:

【参考方案3】:

设置按钮的 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];

【讨论】:

以上是关于IOS - 按钮从右侧动画滑入的主要内容,如果未能解决你的问题,请参考以下文章

SwiftUI 动画滑入和滑出

从导航堆栈中弹出视图控制器时从右侧滑入

UIView 动画滑入

如何在幻灯片中添加滚动动画?

使用 TailwindCSS 单击按钮时,使侧边栏从左侧滑入

在按钮按下颤动时开始滑入动画