iPhone闪耀动画

时间:2011-07-03 18:20:19

标签: iphone objective-c

我正试图让我的观点做一个漂亮的闪亮动画来吸引用户的眼球。任何想法如何实现这个?

这是我到目前为止所拥有的:

[UIView beginAnimations:@"viewShine" context:self.view];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationRepeatCount:0];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
//Do nice shining animation here
[UIView commitAnimations];

通过闪耀,我的意思是像打开iPhone时“滑动解锁”文本或任何易于操作且看起来不错的任何内容。

5 个答案:

答案 0 :(得分:8)

我编写了一个方法,可以在任何UIView上调用它,使其无需包含图像即可获得闪耀:

-(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;
}

答案 1 :(得分:6)

想出来。

这是我的代码,以防你想做类似的事情:

UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[whiteView setBackgroundColor:[UIColor whiteColor]];
[whiteView setUserInteractionEnabled:NO];
[self.view addSubview:whiteView];

CALayer *maskLayer = [CALayer layer];

// Mask image ends with 0.15 opacity on both sides. Set the background color of the layer
// to the same value so the layer can extend the mask image.
maskLayer.backgroundColor = [[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.0f] CGColor];
maskLayer.contents = (id)[[UIImage imageNamed:@"ShineMask.png"] CGImage];

// Center the mask image on twice the width of the text layer, so it starts to the left
// of the text layer and moves to its right when we translate it by width.
maskLayer.contentsGravity = kCAGravityCenter;
maskLayer.frame = CGRectMake(-whiteView.frame.size.width, 
                             0.0f, 
                             whiteView.frame.size.width * 2, 
                             whiteView.frame.size.height);

// Animate the mask layer's horizontal position
CABasicAnimation *maskAnim = [CABasicAnimation animationWithKeyPath:@"position.x"];
maskAnim.byValue = [NSNumber numberWithFloat:self.view.frame.size.width * 9];
maskAnim.repeatCount = HUGE_VALF;
maskAnim.duration = 3.0f;
[maskLayer addAnimation:maskAnim forKey:@"shineAnim"];

whiteView.layer.mask = maskLayer;

使用此图片:

enter image description here

答案 2 :(得分:2)

以下是闪光效果的快速代码

class Animate {

    /// Add a persistent shimmer animation. Usage: `Animate.shimmer(myView)`
    static func shimmer(view: UIView) {
      let gradient = CAGradientLayer()
      gradient.startPoint = CGPointMake(0, 0)
      gradient.endPoint = CGPointMake(1, -0.02)
      gradient.frame = CGRectMake(0, 0, view.bounds.size.width*3, view.bounds.size.height)

      let lowerAlpha: CGFloat = 0.7
      let solid = UIColor(white: 1, alpha: 1).CGColor
      let clear = UIColor(white: 1, alpha: lowerAlpha).CGColor
      gradient.colors     = [ solid, solid, clear, clear, solid, solid ]
      gradient.locations  = [ 0,     0.3,   0.45,  0.55,  0.7,   1     ]

      let theAnimation : CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.x")
      theAnimation.duration = 4
      theAnimation.repeatCount = Float.infinity
      theAnimation.autoreverses = false
      theAnimation.removedOnCompletion = false
      theAnimation.fillMode = kCAFillModeForwards
      theAnimation.fromValue = -view.frame.size.width * 2
      theAnimation.toValue =  0
      gradient.addAnimation(theAnimation, forKey: "animateLayer")

      view.layer.mask = gradient

    }
}
  

Swift 3.0

func shimmer(view: UIView) {
        let gradient = CAGradientLayer()
        gradient.startPoint = CGPoint(x: 0, y: 0)
        gradient.endPoint = CGPoint(x: 1, y: -0.02)
        gradient.frame = CGRect(x: 0, y: 0, width: view.bounds.size.width*3, height: view.bounds.size.height)

        let lowerAlpha: CGFloat = 0.7
        let solid = UIColor(white: 1, alpha: 1).cgColor
        let clear = UIColor(white: 1, alpha: lowerAlpha).cgColor
        gradient.colors     = [ solid, solid, clear, clear, solid, solid ]
        gradient.locations  = [ 0,     0.3,   0.45,  0.55,  0.7,   1     ]

        let theAnimation : CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.x")
        theAnimation.duration = 2
        theAnimation.repeatCount = Float.infinity
        theAnimation.autoreverses = false
        theAnimation.isRemovedOnCompletion = false
        theAnimation.fillMode = kCAFillModeForwards
        theAnimation.fromValue = -view.frame.size.width * 2
        theAnimation.toValue =  0
        gradient.add(theAnimation, forKey: "animateLayer")

        view.layer.mask = gradient
    }

答案 3 :(得分:1)

值得一提的是:如果是闪亮的进度标签,例如在facebook纸质应用或iOS锁定屏幕中,您可以考虑使用https://github.com/facebook/Shimmer

答案 4 :(得分:0)

Swift 5.0

void loadKeys() throws Exception{
    KeyPair keyPair = loadKeyPair();

    if (null != keyPair) {
        RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
    }
}