UIButton的压缩与拉伸动画

本文主要完成UIButton的一个拉伸与压缩动画

压缩动画:将半圆角的的按钮压缩成圆形按钮(背景波纹动画单独完成)

拉伸动画;将圆形按钮拉伸成半圆角Button

注:压缩和拉伸过程装,要保证圆角不变形

初始状态:

初始状态/结束状态

压缩状态:

压缩后状态

代码如下:

//带圆角的UIButton压缩成圆形
//圆形UIButton拉伸成带圆角的:圆角不拉伸


- (void) startPkMatchingAction:(UIButton*)sender
{
    if (sender.tag == RHCStarlightPKFree)
    {
        //Button压缩成圆形
        [UIView animateWithDuration:0.5 animations:^{
            self.matchingButton.width = self.matchingButton.height;
            self.matchingButton.centerX = self.width/2;
            [self.matchingButton setText:nil];
        } completion:^(BOOL finished) {

            [self.matchingButton setBackgroundImage:[UIImage imageForKey:@"matchingPKButton_background_matching"] forState:UIControlStateNormal];
            self.explainLabel.text = @"再次点击暂停匹配...";
            self.matchingButton.tag = RHCStarlightPKWait;
        }];
    }else if (sender.tag == RHCStarlightPKWait)
    {
        //圆形Button拉伸成圆角矩形
        self.matchingButton.width = 41;//先将圆形的宽度扩大一点点
        self.matchingButton.centerX = self.width/2;
        [self.matchingButton setBackgroundImage:[UIImage imageForKey:@"matchingPKButton_background"] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.5 animations:^{
           //关键代码:保证圆角不被拉伸
            [self.matchingButton.imageView.image  resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 20) resizingMode:UIImageResizingModeStretch];
            self.matchingButton.width = 150;
            self.matchingButton.centerX = self.width/2;
        } completion:^(BOOL finished) {
           
            [self.matchingButton setTitle:@"开始匹配" forState:UIControlStateNormal];
            self.explainLabel.text = @"*由系统随机匹配好友";
            self.matchingButton.tag = RHCStarlightPKFree;
        }];
    }
   
}
 

猜你喜欢

转载自blog.csdn.net/weixin_39624536/article/details/83713955