iOS开发 飘雪动画、波纹动画、 仿iPhone解锁文字,渐变的文字动画

来源:互联网 发布:java时间转换成毫秒 编辑:程序博客网 时间:2024/05/01 18:01

一、使用NSTimer与iphone的简单动画,实现飘雪效果


使用NSTimer与iphone的简单动画,实现飘雪效果,这理原理比较简单,就是定时生成一定的雪花图片,然后使用动画的方式向下漂落(我在其它论坛,看到使用path的方式实现的一个云漂来漂去的效果,实际也可以用那种方式实现,这实际就是前面说的动画效果的两种应用)。所以,我们可以在viewDidLoad事件中,增加一个图片及定时器并启动,这里的pic请在头文件中定义。
-(void)viewDidLoad{
    [super viewDidLoad];
    self.pic = [UIImageimageNamed:@"snow.png"];//初始化图片
    //启动定时器,实现飘雪效果
    [NSTimer scheduledTimerWithTimeInterval:(0.2)target:self selector:@selector(ontime) userInfo:nilrepeats:YES];
}
然后再实现定时器定时调用的ontime方法:
-(void)ontime{
    UIImageView *view = [[UIImageView alloc]initWithImage:pic];//声明一个UIImageView对象,用来添加图片
    view.alpha = 0.5;//设置该view的alpha为0.5,半透明的
    int x = round(random()20);//随机得到该图片的x坐标
    int y =round(random()20);//这个是该图片移动的最后坐标x轴的
    int s = round(random())+10;//这个是定义雪花图片的大小
    int sp = 1/round(random()0)+1;//这个是速度
    view.frame = CGRectMake(x, -50, s,s);//雪花开始的大小和位置
    [self.view addSubview:view];//添加该view
    [UIView beginAnimations:nilcontext:view];//开始动画
    [UIView setAnimationDuration:10*sp];//设定速度
    view.frame = CGRectMake(y, 500, s,s);//设定该雪花最后的消失坐标
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
}


二、波纹动画

            CABasicAnimation *base = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
            base.duration = 1;
            base.fromValue = [NSNumber numberWithInt:0];
            base.toValue = [NSNumber numberWithInt:1];
            
            CAKeyframeAnimation *key = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
            key.duration = 1;
            key.keyTimes = @[@0,@0.05,@1];
            key.values = @[@0,@1,@0];
            
            CAAnimationGroup *group = [CAAnimationGroup animation];
            group.animations = @[base,key];
            group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
            group.duration = 1;
            group.repeatCount = HUGE_VALF;
            group.removedOnCompletion = NO;
            
            
            CAShapeLayer *layer = [CAShapeLayer layer];
            layer.position = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
            layer.bounds = CGRectMake(0, 0, 200, 200);
            //    layer.anchorPoint = CGPointMake(0.5, 0.5);
            layer.backgroundColor =[UIColor clearColor].CGColor;
            UIBezierPath *path = [UIBezierPath bezierPath];
            [path addArcWithCenter:(CGPointMake(100, 100)) radius:100 startAngle:0 endAngle:360*M_PI/180 clockwise:false];
            layer.fillColor = [UIColor redColor].CGColor;
            layer.path = path.CGPath;
            [layer addAnimation:group forKey:nil];
            [self.view.layer addSublayer:layer];



三、仿iPhone解锁文字,渐变的文字动画

UILabel *iphoneFade = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
iphoneFade.text = @"中华人民共和国";
iphoneFade.font = [UIFont systemFontOfSize:30];
iphoneFade.textColor = [UIColor redColor];
iphoneFade.center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0+50);
[self.view addSubview:iphoneFade];


UILabel *iphoneFade1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
iphoneFade1.text = @"中华人民共和国";
iphoneFade1.font = [UIFont systemFontOfSize:30];
iphoneFade1.textColor = [UIColor whiteColor];
iphoneFade1.center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0+50);
[self.view addSubview:iphoneFade1];



CAGradientLayer *layer = [CAGradientLayer layer];
layer.frame = CGRectMake(0, 0, 300, 40);
layer.colors = @[(id)[UIColor clearColor].CGColor,(id)[UIColor greenColor].CGColor,(id)[UIColor clearColor].CGColor];//三种颜色
layer.locations = @[@(0.25),@(0.5),@(0.75)];//三总颜色各自占得大小,范围0~1;//类别2
//    layer.locations = @[@(0.01),@(0.1),@(0.9),@(0.99)];//类别1
layer.startPoint = CGPointMake(0, 0);
layer.endPoint = CGPointMake(1, 0);
iphoneFade1.layer.mask = layer;
layer.position = CGPointMake(-300/4.0, 40/2.0);//类别2



CABasicAnimation *basicAnimation = [CABasicAnimation animation];
basicAnimation.keyPath = @"transform.translation.x";
basicAnimation.fromValue = @(0);
basicAnimation.toValue = @(300+300/2.0);//类别2
//    basicAnimation.toValue = @(300);//类别1
basicAnimation.duration = 2;
basicAnimation.repeatCount = LONG_MAX;
basicAnimation.removedOnCompletion = NO;
basicAnimation.fillMode = kCAFillModeForwards;

[iphoneFade1.layer.mask addAnimation:basicAnimation forKey:nil];



0 0
原创粉丝点击