UIView 实现动画

参考这个帖子,主要讲了用UIView来实现动画。

http://www.tuicool.com/articles/BjMrQne

其中比较好用的

(一)代码块,此处实现了滑块的平移

    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
    v.backgroundColor = [UIColor redColor];
    [self.view addSubview:v];
    [UIView animateWithDuration:1 animations:^{
        CGPoint p = v.center;
        p.y += 100;
        v.center = p;
    } completion:^(BOOL finished) {
        CGPoint p = v.center;
        p.y -= 100;
        v.center = p;
    }];

(二)连续动画帧,这个感觉还是比较麻烦的

   UIView *v = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)]; 
    void (^keyFrameBlock)() = ^(){
        // 创建颜色数组
        NSArray *arrayColors = @[[UIColor orangeColor],
                                 [UIColor yellowColor],
                                 [UIColor greenColor],
                                 [UIColor blueColor],
                                 [UIColor purpleColor],
                                 [UIColor redColor]];
        NSUInteger colorCount = [arrayColors count];
        // 循环添加关键帧
        for (NSUInteger i = 0; i < colorCount; i++) {
            [UIView addKeyframeWithRelativeStartTime:i / (CGFloat)colorCount
                                    relativeDuration:1 / (CGFloat)colorCount
                                          animations:^{
                                              [v setBackgroundColor:arrayColors[i]];
                                          }];
        }
    };
    [UIView animateKeyframesWithDuration:4.0
                                   delay:0.0
                                 options:UIViewKeyframeAnimationOptionCalculationModeCubic | UIViewAnimationOptionCurveLinear
                              animations:keyFrameBlock
                              completion:^(BOOL finished) {
                                  // 动画完成后执行
                                  // code...
                              }];
    

 

(三)图片帧动画,这个在其他地方看到,好用且简单(通过动画更新图片的显示,再加上定时器去设置图片的显示位置)

 //动画的原理:在短时间内播放足够多的图片.
    //形成动画的最小要求:24帧/秒
    UIImage *image = [UIImage imageNamed:@"DOVE 1"];
    
    //创建一个显示图片视图
    UIImageView *birdView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 150, image.size.width, image.size.height)];
    
    [self.window addSubview:birdView];
    
    NSMutableArray *birds = [NSMutableArray array];
    
    for (int i = 1; i<19; i++) {
        NSString *string = [NSString stringWithFormat:@"DOVE %i",i];
        UIImage *subImage = [UIImage imageNamed:string];
        
        [birds addObject:subImage];
    }
    
    //将所有的鸟图片添加到显示图片视图
    [birdView setAnimationImages:birds];
    //动画持续时间
    [birdView setAnimationDuration:0.5];
    //设置动画重复次数  默认是1次   0代表无数次
    [birdView setAnimationRepeatCount:0];
    
    //开启动画
    [birdView startAnimating];
    
     birdView.tag = 20;
    
    //使用定时器让鸟移动
    /*
     1.间隔时间
     2.方法对应的对象
     3.调用的方法
     4.附加信息
     5.是否重复执行
     */
    [NSTimer scheduledTimerWithTimeInterval:1/60. target:self selector:@selector(birdsFly:) userInfo:birdView repeats:YES];
    
}

-(void)birdsFly:(NSTimer *)timer{
    //通过视图的tag取出子视图
//    UIImageView *birdView = (UIImageView *)[self.window viewWithTag:20];
    //方法二:利用NStimer的userInfo属性
    UIImageView *birdView = timer.userInfo;
    //语法错误
//    birdView.frame.origin.x = 4;
    CGRect frame = birdView.frame;
    
    frame.origin.x += 2;
    
    if (birdView.frame.origin.x >= 320) {
        frame.origin.x = -birdView.frame.size.width;
    }
    
    //整体赋值是可以的
    birdView.frame = frame;
}

 

posted @ 2016-07-01 18:52  知吾猪  阅读(229)  评论(0编辑  收藏  举报