iPhone shine animation

前端 未结 5 1886
北海茫月
北海茫月 2021-02-01 10:53

I\'m trying to get my view to do a nice shining animation to catch the user\'s eyes. Any ideas how to implement this?

Here\'s what I have so far:

[UIView         


        
5条回答
  •  余生分开走
    2021-02-01 11:28

    I wrote a method that can be called on any UIView to give it the shine you are looking for without needing to include an image:

    -(void)AddShineAnimationToView:(UIView*)aView
    {
        CAGradientLayer *gradient = [CAGradientLayer layer];
        [gradient setStartPoint:CGPointMake(0, 0)];
        [gradient setEndPoint:CGPointMake(1, 0)];
        gradient.frame = CGRectMake(0, 0, aView.bounds.size.width*3, aView.bounds.size.height);
        float lowerAlpha = 0.78;
        gradient.colors = [NSArray arrayWithObjects:
                           (id)[[UIColor colorWithWhite:1 alpha:lowerAlpha] CGColor],
                           (id)[[UIColor colorWithWhite:1 alpha:lowerAlpha] CGColor],
                           (id)[[UIColor colorWithWhite:1 alpha:1.0] CGColor],
                           (id)[[UIColor colorWithWhite:1 alpha:1.0] CGColor],
                           (id)[[UIColor colorWithWhite:1 alpha:1.0] CGColor],
                           (id)[[UIColor colorWithWhite:1 alpha:lowerAlpha] CGColor],
                           (id)[[UIColor colorWithWhite:1 alpha:lowerAlpha] CGColor],
                           nil];
        gradient.locations = [NSArray arrayWithObjects:
                              [NSNumber numberWithFloat:0.0],
                              [NSNumber numberWithFloat:0.4],
                              [NSNumber numberWithFloat:0.45],
                              [NSNumber numberWithFloat:0.5],
                              [NSNumber numberWithFloat:0.55],
                              [NSNumber numberWithFloat:0.6],
                              [NSNumber numberWithFloat:1.0],
                              nil];
    
        CABasicAnimation *theAnimation;
        theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
        theAnimation.duration = 2;
        theAnimation.repeatCount = INFINITY;
        theAnimation.autoreverses = NO;
        theAnimation.removedOnCompletion = NO;
        theAnimation.fillMode = kCAFillModeForwards;
        theAnimation.fromValue=[NSNumber numberWithFloat:-aView.frame.size.width*2];
        theAnimation.toValue=[NSNumber numberWithFloat:0];
        [gradient addAnimation:theAnimation forKey:@"animateLayer"];
    
        aView.layer.mask = gradient;
    }
    

提交回复
热议问题