zoukankan      html  css  js  c++  java
  • ios--计时器演示样例:一闪一闪亮晶晶(动画)

    本演示样例实现的动画:UIView定时消失随后又闪现,即一闪一闪的动画

    所採用的技术:定时器(NSTimer) + 动画(beginAnimations/commitAnimations)

    详细实现步骤:

    第一步:定时器部分全然复制上个演示样例:http://blog.csdn.net/wanggsx918/article/details/38269919

    1、在.h文件里定义一个变量和一个Method:

    @interface xxxViewController : UIViewController
    {
        NSTimer *showTimer;//计时器变量
    }
    //要运行的方法
    -(void)handleScrollTimer:(NSTimer *)theTimer;     -(void)startTimer;

    
    

    2、在.m文件里打开与关闭定时器,以及绑定Method:

    - (void)viewDidAppear:(BOOL)animated  
    {
        ///页面显示完成的时候运行
        //又一次打开定时器
        [showTimer setFireDate:[NSDate distantPast]];
    }
    
    
    -(void)startTimer
    {
        //定义时间计数器:每隔2秒运行一次handleScrollTimer方法
        showTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
                                                     target:self
                                                   selector:@selector(handleScrollTimer:)
                                                   userInfo:nil
                                                    repeats:true];
        [[NSRunLoop currentRunLoop] addTimer:showTimer forMode:NSDefaultRunLoopMode];
    }
    
    ///页面消失完成的时候运行
    -(void)viewDidDisappear:(BOOL)animated
    {
        //关闭定时器
        [showTimer setFireDate:[NSDate distantFuture]];
    }

    3、启动计时器:

        //开启线程
        [self performSelectorOnMainThread:@selector(startTimer) withObject:nil waitUntilDone:YES];



    第二步:动画部分

    handleScollTimer:方法中写动画代码:

    -(void)handleScrollTimer:(NSTimer *)theTimer
    {
        scanLine.alpha = 1.0;
        [UIView beginAnimations:@"scanLine" context:nil];
        [UIView setAnimationDuration:0.8];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        scanLine.alpha = 0.05;
        [UIView commitAnimations];
    }


  • 相关阅读:
    python 29day--异常处理及socket简介
    python 28day--类的总结
    python 27day--类的内置函数补充
    python 26day-- 面向对象的三大特性
    python 25day--面对对象进阶
    python 24day--python面向对象编程
    python 23day--python模块的应用
    python 22day--python的模块与包
    python 21day--文件的增删改查功能实现
    虚拟机的三种模式
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6790028.html
Copyright © 2011-2022 走看看