简体   繁体   English

如何在 iOS 中实现矩形 UIButton 到圆形的漂亮动画?

[英]How to implement nice animation for rectangle UIButton to circle shape in iOS?

When i clicked on "Login" button i need to make my rectangle login button Login Screenshot to animate and change it shape to circle like below link Login Button after i click当我点击“登录”按钮时,我需要制作我的矩形登录按钮登录屏幕截图以进行动画处理并将其形状更改为如下所示的圆形链接登录按钮单击后

I have seen this code in stackoverflow but its not working the way i want我在 stackoverflow 中看到过这段代码,但它没有按照我想要的方式工作

    CGFloat width = self.login.frame.size.width;
CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
morph.fromValue = @0;
morph.toValue = @(width/2);
morph.duration = 0.5;
[self.login.layer addAnimation:morph forKey:@"morph"];
self.login.layer.cornerRadius = width/2;

Please help请帮忙

Thanks for answering this.感谢您回答这个问题。

This is the final code这是最终的代码

[UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationCurveLinear
                     animations:^{
                         self.layoutWidth.constant = 70;
                         [self.view layoutIfNeeded];
                         self.btnLogin.clipsToBounds = YES;
                         self.btnLogin.layer.cornerRadius =self.btnLogin.bounds.size.height/2.0f;

                     }
                     completion:^(BOOL finished){


                     }];

How about this?这个怎么样?

 [UIView animateWithDuration:1.0 animations:^{
        CGRect frame = btn.frame;
        CGPoint center = btn.center;
        frame.size.width = btn.frame.size.height;
        frame.size.height = btn.frame.size.height;

        btn.frame = frame;
        btn.center = center;
        btn.layer.cornerRadius = frame.size.width/2;
        btn.clipsToBounds = YES;

    }];

This is what i get这就是我得到的在此处输入图片说明

And after a 1 sec animation i get this在 1 秒动画后,我得到了这个

在此处输入图片说明

Try this,尝试这个,

Demo Animation演示动画

Code:代码:

- (IBAction)clicklogin:(id)sender {

    [UIView animateWithDuration:0.1
                          delay:1.0
                        options:UIViewAnimationCurveLinear
                     animations:^{
                         self.layoutWidth.constant = 70;
                         [self.view layoutIfNeeded];

                     }
                     completion:^(BOOL finished){


                         NSLog(@"Done!");

                         [UIView animateWithDuration:0.1
                                               delay:0.0
                                             options:UIViewAnimationCurveLinear
                                          animations:^{
                                              self.btnLogin.clipsToBounds = YES;
                                              self.btnLogin.layer.cornerRadius =self.btnLogin.bounds.size.height/2.0f; //or use ( self.login.bounds.size.height/2);
                                              self.btnLogin.layer.borderColor=[UIColor redColor].CGColor;
                                              self.btnLogin.layer.borderWidth=2.0f;

                                              [self.view layoutIfNeeded];

                                          }
                                          completion:^(BOOL finished){


                                              NSLog(@"Done!");

                                          }];


                     }];

}

Output :输出 :

在此处输入图片说明

If you want to get circle,just follow simple way如果你想得到圈子,只需按照简单的方法

-(IBAction)actionClick:(id)sender
{  
  [UIView animateWithDuration:0.7f
                 animations:^
  {
     [sender setAlpha:0.5f];
  }
                 completion:^(BOOL finished)
  {
     [sender setAlpha:1.0f];
     button.layer.cornerRadius = button.frame.size.width / 2;
     button.layer.borderColor=[UIColor redColor].CGColor;
     button.layer.borderWidth=2.0f;
    ];
  }
}

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

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