UIView动画

2016-05-22  本文已影响78人  Barry_小闪

目录

定时器动画效果

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(ytTransform:) userInfo:@"aaa" repeats:YES];

形变

1.旋转(当前视图上的子视图也会跟着一起旋转)

[view1 setTransform:CGAffineTransformMakeRotation(M_PI)];

2.缩放(当前视图的子视图也会一起缩放)

3.平移

[view1 setTransform:CGAffineTransformMakeTranslation(0, 0)];

4.时时旋转

static float rotation = M_PI;
[view setTransform:CGAffineTransformMakeRotation(rotation +  M_PI/10)];
rotation += M_PI/10;

5.时时缩放

  static float scale = 1;
  static float scaleValue = 1;

  //时时缩放  
  [view setTransform:CGAffineTransformMakeScale(scale + (0.2)*scaleValue, scale + (0.2)*scaleValue)];

  缩放后更新缩放当前的比例
  scale += 0.2 * scaleValue;
  
  当缩放比例大于4的时候让比例减小
   if (scale >= 4) {
       
     scaleValue = -1;
     }
   
  当缩放比例小于0.2的时候让比例增加
     if (scale < 0.2) {
       
   scaleValue = 1;
     }

6.时时平移

    static float translation = 0;
    static float translationValue = 1;
    
    [view setTransform:CGAffineTransformMakeTranslation(translation + 10 * translationValue, 0)];
.
    //更新当前平移的距离
    translation += 10 * translationValue;
    
    if (view.frame.origin.x <= 0) {
        translationValue = 1;
    }
    
    if (view.frame.origin.x >= _window.frame.size.width - view
        .frame.size.width) {
        
        translationValue = -1;
    }
  [UIView animateWithDuration:0.3 animations:^{
       
        //动画结果
        
  }];

block动画效果

参数1:动画时间
参数2:block,用来实现动画结果的代码段
参数3:block,动画结束后需要执行的代码段

 [UIView animateWithDuration:0.3 animations:^{
        //动画结果
 } completion:^(BOOL finished) {
        //finished:是否结束
        //动画结束后执行的代码段
 }];

=====================================
1.创建一个视图

 UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
 view.backgroundColor = [UIColor redColor];
 [self.window addSubview:view];
    ```

2.使用动画
```objc
  [UIView animateWithDuration:1 animations:^{
        
  使用动画效果改变视图的frame
  view.frame = CGRectMake(100, 500, 100, 100);
        
  使用动画效果改变视图的背景色
  view.backgroundColor = [UIColor blackColor];
        
  使用动画效果改变视图的透明度
  透明度:0 - 1 (改变父视图的透明度,子视图的透明度会跟着改变;如果只想改变父视图透明,就改变父视图背景颜色的透明度)
  view.alpha = 0.2;
        
  使用动画效果改变视图的形变
  view.transform = CGAffineTransformMakeScale(0.5, 0.5);
        
    }];
  [UIView animateWithDuration:0.5 animations:^{
        
      //动画结果
      view.transform = CGAffineTransformMakeScale(0.2, 0.2);
        
  } completion:^(BOOL finished) {
       
      //动画结束后移除视图
      NSLog(@"动画结束");
     [view removeFromSuperview];
        
     //变小的动画结束后,再变回去
     [UIView animateWithDuration:1.5 animations:^{
            
      view.transform = CGAffineTransformMakeScale(1, 1);

    }];  
}];
上一篇 下一篇

猜你喜欢

热点阅读