Fork me on GitHub

UI-定时器与动画使用总结

#pragma mark - 定时器   ************************************************************************************

   //0.创建一个以下延时使用的方法

    - (void)delayMethod { NSLog(@"execute"); }

    1.performSelector方法================================================================

    [self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0f];

    此方式要求必须在主线程中执行,否则无效。

    是一种非阻塞的执行方式,

    暂时未找到取消执行的方法。   

 

    2.定时器:NSTimer==================================================================

     创建定时器

    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(delayMethod ) userInfo:nil repeats:NO];

    把定时器 添加到当前主运行循环中

    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

    

    下面这种方法创建的定时器,会自动的加入运行循环

    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];

    此方式要求必须在主线程中执行,否则无效。

    是一种非阻塞的执行方式,

    可以通过NSTimer类的- (void)invalidate;取消执行。    

 

    3. sleep方式==============================================================================

    sleep(1.0f);    

    [NSThread sleepForTimeInterval:1.0f]; [self delayMethod];

    此方式在主线程和子线程中均可执行。

    是一种阻塞的执行方式,建方放到子线程中,以免卡住界面

    没有找到取消执行的方法。

    

    4.GCD方式  ===========================================================================  

    double delayInSeconds = 1.0;

    __block ViewController* bself = self;

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));

    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        [bself delayMethod]; });

    此方式在可以在参数中选择执行的线程。

    是一种非阻塞的执行方式,

    没有找到取消执行的方法。

 

#pragma mark - 1. 动画************************************************************************************

    一. 通过定时播放多张图片动画=====================================================

    1.1 定义 UIImageView

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    

    1.2 定义 NSMutableArray 保存所有图片

    NSMutableArray *animationImgsArrM = [[NSMutableArray alloc] init];

    int num = 40;

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

        图片的名称

        NSString *imgName = [NSString stringWithFormat:@"%@_%02d.jpg", imgView, i ];

        在创建比较小的,并且常用的图片时,可以使用imageNamed:方法创建

        UIImage *img = [UIImage imageNamed:imgName];

        

        如果创建的图片很多,并且比较大的话,应该使用这种方式创建

        NSString *filePath = [[NSBundle mainBundle] pathForResource:imgName ofType:nil];

        UIImage *img = [[UIImage alloc]initWithContentsOfFile:filePath];

        把图片添加到数组中

        [animationImgsArrM addObject:img];

    }

   

    设置动画属性

    imgView.animationImages = animationImgsArrM;    //动画内容

    imgView.animationDuration = num * 0.08;         //动画(总)时间间隔

    imgView.animationRepeatCount = 1;               //动画重复次数

 ..............

    

    开始动画

    [imgView startAnimating];

    

    动画播放完成后,清除数组中的图片

    1.  [self performSelector:@selector(clearImages) withObject:nil afterDelay:imgView.animationDuration];

    2.  [imgView performSelector:@selector(setAnimationImages:) withObject:nil

                   afterDelay:imgView.animationDuration];    

  动画播放完成后,清除数组中的图片。        让imgView控件在动画完成后执行setAnimationImages:方法,该方法默认参数为nil

    二.通过定时播放较少图片动画 , 通常在改变某一控件的frame时使用===================================

    UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

    imgView1.image = [UIImage imageNamed:@"head"];

     1. 开始动画

    [UIView beginAnimations:nil context:nil];

    2. 动画持续2秒

    [UIView setAnimationDuration:2.0];    

    2.1 取出原来的属性

    CGPoint tempCenter = imgView1.center;

    2.2 修改临时属性

    tempCenter.y -= 200;

    2.3 重新赋值

    imgView1.center = tempCenter; 

    3. 提交动画

    [UIView commitAnimations];

    

    三. 通过block 创建动画 可以通过[UIView anima...]设置想要设置的某种动画=========================

    

    [UIView animateWithDuration:2 animations:^{

        

        imgView1.transform = CGAffineTransformMakeTranslation(0, 100 );

    }];

    [UIView animateWithDuration:<#(NSTimeInterval)#> animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>];   

 

posted @ 2016-04-10 10:00  极度恐慌_JG  阅读(245)  评论(0编辑  收藏  举报