前言
在IOS客户端开发中,用到动画的地方比较少,如果是要把app做的很酷炫,那么就有必要用一些动画效果。
一、最普通的动画
1、下面这段代码是最简单的动画,只是实现了简单的位置移动和变化
- (void)viewDidLoad { [super viewDidLoad]; UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; imgView.image = [UIImage imageNamed:@"123.jpg"]; [self.view addSubview:imgView]; [UIView beginAnimations:nil context:nil]; //设置动画开始 [UIView setAnimationDuration:3.0];//设置整个动画的时间 CGRect frame = imgView.frame; frame.origin.x += 150; frame.origin.y += 100; [imgView setFrame:frame]; [UIView commitAnimations]; //动画结束 }
这种动画,我觉得可以通俗的理解为视图或者控件出场的动画效果。
- (void)viewDidLoad { [super viewDidLoad]; UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; imgView.image = [UIImage imageNamed:@"123.jpg"]; [self.view addSubview:imgView]; CATransition *animation = [CATransition animation]; animation.duration = 0.5f; //设置动画的时间 animation.timingFunction = UIViewAnimationCurveEaseInOut; //设置动画的方式 animation.fillMode = kCAFillModeBackwards; animation.type = kCATransitionMoveIn; animation.subtype = kCATransitionFromLeft; [imgView.layer addAnimation:animation forKey:@"animation"]; }
- (void)viewDidLoad { [super viewDidLoad]; UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; imgView.image = [UIImage imageNamed:@"123.jpg"]; [self.view addSubview:imgView]; [UIView animateWithDuration:3.25 animations:^{ CGAffineTransform newTransform = CGAffineTransformMakeScale(2.2, 2.2); [imgView setTransform:newTransform]; }completion:^(BOOL finished){ [imgView removeFromSuperview]; }]; }