iOS UIView简单缩放动画

@interface ViewController () {
    UIView *animationView;
    UIButton *button;
    CGPoint animationPoint;
}

@end

 

初始化button和动画的view

复制代码
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 创建Button
    button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.layer.borderWidth = 0.5f;
    button.layer.cornerRadius = 7.0f;
    button.frame = CGRectMake(240, 50, 60, 25);
    [button setTitle:@"动画" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(showAnimation) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    //  动画缩放开始的点
    animationPoint = CGPointMake(button.frame.origin.x + button.frame.size.width / 2, 0);
    
    //  动画view
    animationView = [[UIView alloc] initWithFrame:CGRectMake(20, button.frame.origin.y + button.frame.size.height + 10, 280, 100)];
    animationView.backgroundColor = [UIColor grayColor];
    animationView.layer.cornerRadius = 7.0f;
    animationView.alpha = 0.0f;
    [self.view addSubview:animationView];
}
复制代码

 

动画的处理方法

复制代码
- (void)showAnimation {
    
    CGFloat offsetX = animationPoint.x - self.view.frame.size.width / 2;
    CGFloat offsetY = animationPoint.y - animationView.frame.size.height / 2;

    if (animationView.alpha == 0.0f) {
        // 动画由小变大
        animationView.transform = CGAffineTransformMake(0.01, 0, 0, 0.01, offsetX, offsetY);
        
        [UIView animateWithDuration:0.3f animations:^{
            animationView.alpha = 1.0f;
            animationView.transform = CGAffineTransformMake(1.05f, 0, 0, 1.0f, 0, 0);
            
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.1f animations:^{
                animationView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
            } completion:^(BOOL finished) {
                //  恢复原位
                animationView.transform = CGAffineTransformIdentity;
            }];
        }];
        
    } else {
        
        // 动画由大变小
        animationView.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);
        [UIView animateWithDuration:0.2 animations:^{
            animationView.transform = CGAffineTransformMake(0.01, 0, 0, 0.01, offsetX, offsetY);
        } completion:^(BOOL finished) {
            animationView.transform = CGAffineTransformIdentity;
            animationView.alpha = 0.0f;
        }];
    }
    
}
复制代码

 

动画效果图

 

posted @   wb145230  阅读(13892)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示