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; }]; } }
动画效果图