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

}

动画效果图

时间: 2024-07-29 09:28:32

iOS UIView简单缩放动画的相关文章

iOS UIView动画详解(Objective-C)

我在之前的一篇博客中<iOS UIView动画详解(Swift)>讲解了使用Swift来实现UIView类下面提供的多种动画效果,如位置动画.旋转动画.缩放动画.颜色动画.透明度动画等等.为了这个题目的完整性,今天我使用Objective-C来完全重写以上的所有的动画.项目案例已经上传至:https://github.com/chenyufeng1991/iOS-UIView-Animation  中的Animation-OC文件夹下,另一个目录下则是Swift实现的动画. (1)位置动画 P

iOS UIView动画详解

现在的iOS开发中,有很多的动画框架可以使用,包括苹果自带的CoreAnimation框架,Facebook的Pop等等,这些的确都是程序员的利器.但是如果我们仅仅是想要实现一些比较简单的动画呢?杀鸡焉用牛刀.我们直接用UIView就可以了.今天我们就来好好聊聊UIView动画,使用Swift编写(大家可以看到我有时候用OC,有时候用Swift,现在的iOS学习的基本技能看着OC代码能写出Swift,照着Swift能写出OC,哈哈).本示例代码上传至  https://github.com/ch

iOS UIView动画实践(一):揭开Animation的神秘面纱

前言 在一个看脸的社会中,不论什么事物,长得好看总是能多吸引一些目光.App同样不例外,一款面相不错的App就算功能已经被轮子千百遍,依然会有人买账,理由就是看得顺眼,于是平面设计人员越来越被重视.白驹过隙,斗转星移,人们已然不满足于静态的美感,于是动态的用户体验应运而生,平面设计人员捉襟见肘,是我们程序员出马的时候了. 这篇文章是UIView Animation的第一篇,从极简的概念开始,为大家揭开Animation的神秘面纱.我们以一个登录界面为例.美丽的太阳,婀娜的云,还有几个小山包,中间

文顶顶 iOS开发UI篇—iOS开发中三种简单的动画设置

iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所

iOS开发UI篇—iOS开发中三种简单的动画设置

iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所

ios 动画学习(-)UIView 自带动画

ios 开发中,一些常用的简单的动画可以用 uivew 自带的动画来是实现 今天就学习下 UIVIew 一些常用的动画 1. // 翻转的动画 [UIView beginAnimations:@"doflip" context:nil]; //设置时常 [UIView setAnimationDuration:1]; //设置动画淡入淡出 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; //设置代理 [UIView s

ios uiview封装动画(摘录)

iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画支持 执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视图,为此需要将改变属性的代码放在[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之间 常见方法解析: + (void)setAnimat

iOS开发-三种简单的动画设置

[在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所有动画提交并生成动画 [UIView commit

iOS开发中三种简单的动画设置

iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所有动画提交并生成动