码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UIView简单缩放动画

时间:2015-05-14 13:36:47      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

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

 

动画效果图

技术分享

 

iOS UIView简单缩放动画

标签:

原文地址:http://www.cnblogs.com/wb145230/p/4502930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!