动画

点击某个按钮:

使图片向上移

- (void)up:(UIButton *)sender{

//OC语法规定:不允许直接修改 某个对象中结构体属性的成员 错误写法:_imageView.frame.origin.y-=10;

//0、动画(头部-开始动画)

[UIView beginAnimations:nil context:nil];

//设置动画的执行时间

[UIView setAnimationDuration:2.0];

//1、先取出frame

CGRect frame = _imageView.frame;

//2、修改y值

frame.origin.y- = 10;

//3、重新赋值按钮的frame

_imageView.frame = frame;

//4、动画(尾部-提交动画-执行动画)

[UIView commitAnimations];

}

使图片向左旋转

-(void)leftRotate:(UIButton *)sender{

  //弧度  3.14 

  //角度 180

  //向左旋转45°

  [UIView beginAnimations:nil context:nil];

  [UIView setAnimationDuration:2.0];

  _imageView.transform = CGAffineTransformRotate(_imageView.transform, -M_PI_2);

  [UIView commitAnimations];

}

使图片变大

-(void)big:(UIButton *)sender{

  [UIView beginAnimations:nil context:nil];

  [UIView setAnimationDuration:2.0];

  //放大、缩小

  CGFloat scale = sender.tag == 20 ? 1.2 : 0.8;

  _imageView.transform = CGAffineTransformScale(_imageView.transform, scale, scale);

  [UIView commitAnimations];

}

重构代码:(使用block:头部、尾部一样中间不一样时,用block)

-(void)btnClickWithBlock:(void(^)())block{

  [UIView beginAnimations:nil context:nil];

  [UIView setAnimationDuration:2.0];

  block();

}

- (void)up:(UIButton *)sender{

  [self btnClickWitnBlock:^{

  CGRect frame = _imageView.frame;

  frame.origin.y- = 10;

  _imageView.frame = frame;

}];

}

-(void)leftRotate:(UIButton *)sender{

  [self btnClickWitnBlock:^{

  _imageView.transform = CGAffineTransformRotate(_imageView.transform, -M_PI_2);

}];

}

 

//清空之前所有的形变状态(消除以前的旋转、缩放等状态)

_imageView.transform = CGAffineTransformIdentity;

 

序列帧动画

-(void)buttonPressed{

  //1、创建可变数组

  NSMutableArray *image = [NSMutableArray array];

  //2、添加图片

  for (int i=0;i<28;i++) {

  NSString *name = [NSString stingWithFormate:@"%02d.jpg",i];

   UIImage *img = [UIImage imageNamed:name];

  [images addObject:img];

}

  //3、设置动画图片(有顺序)

  _tom.animationImages = images;//序列帧动画

  //4、只播放一次

  _tom.animationRepeatCount = 1;

  //5、设置动画的持续时间

  _tom.animationDuration = 0.1 * count;  

  //6、开始动画

  [_tom startAnimating];

}

posted @ 2015-03-19 18:39  yedandn  阅读(104)  评论(0)    收藏  举报