简体   繁体   English

iOS-按钮从右侧动画滑入

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

i am new to IOS programming. 我是IOS编程的新手。 i have a background image on my screen. 我的屏幕上有背景图片。 What i want is when app launches screen shows with the background image and then after 1 sec 2 buttons comes up from right side of the screen. 我想要的是当应用启动时屏幕上显示背景图像,然后1秒钟后,屏幕右侧出现2个按钮。 how can i do this. 我怎样才能做到这一点。 here is the image what i am trying to do 这是我正在尝试做的图像

在此处输入图片说明

button 1 and button2 have to be invisible first. 按钮1和button2必须先不可见。 after 1 second they comes up from right side. 1秒后,它们从右侧出现。 if there are any tutorials related to this kind of animation then please share 如果有任何与这种动画有关的教程,请分享

You can use simple UIView Animations to achieve this. 您可以使用简单的UIView动画来实现此目的。

Set your button x origin any value greater than screen size so that it is not visible first. 将您的按钮x原点设置为大于屏幕大小的任何值,以使其首先不可见。 Then reset the button frame to a visible value with view animation. 然后使用视图动画将按钮框重置为可见值。 For example call the following function in your viewWillAppear: method. 例如,在您的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!");
                     }];
}

Refer Tutorial 参考教程

Set the buttons' x co-ordinates such that the button is just outside the screen on the right side. 设置按钮的x坐标,以使按钮位于屏幕的右侧。

And then try the following in your viewDidLoad() 然后在您的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];

You can do it this way, please note that, if you want to animate the way your button shows up, you must hide them using alpha = 0.0f (and not hidden = true ). 您可以通过这种方式进行操作,请注意,如果要对按钮的显示方式进行动画处理,则必须使用alpha = 0.0f (而不是hidden = true )将其hidden = true This way, you will have a smooth showing up animation ! 这样,您将可以流畅地显示动画!

Here it is : 这里是 :

First, on your viewDidLoad of your UIViewController , you have to set up a NSTimer of 1sec, then let it call the method that will let your buttons appear : 首先,在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];
}

Ask if you have any questions ! 询问您是否有任何疑问!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM