欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iOS 视图抖动动画、视图旋转动画以及弹框动画效果

程序员文章站 2024-03-26 08:16:56
...

UIView/UIImageView等View 抖动效果


/**
 创建视图抖动效果
 
 @param view 控件
 */
+ (void)shakeAnimationForView:(UIView *) view{
    CALayer *viewLayer = view.layer;// 获取到当前的View
    CGPoint position = viewLayer.position;// 获取当前View的位置
    CGPoint x = CGPointMake(position.x + 6, position.y);// 移动的两个终点位置
    CGPoint y = CGPointMake(position.x - 6, position.y);// 移动的两个终点位置
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];// 设置动画
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];// 设置运动形式
    [animation setFromValue:[NSValue valueWithCGPoint:x]];// 设置开始位置
    [animation setToValue:[NSValue valueWithCGPoint:y]];// 设置结束位置
    [animation setAutoreverses:YES];// 设置自动反转
    [animation setDuration:.08];// 设置时间
    [animation setRepeatCount:4];// 设置次数
    [viewLayer addAnimation:animation forKey:nil];// 添加上动画
}

UIView/UIImageView等View 旋转效果


/**
 创建视图旋转效果
 
 @param view 控件
 */
+ (void)mPiAnimatuinForView:(UIView*)view{
    CABasicAnimation *layer = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    layer.toValue = @(2*M_PI);
    layer.removedOnCompletion = false;
    layer.repeatCount = MAXFLOAT;
    // 设置时间
    [layer setDuration:.08];
    // 设置次数
    [layer setRepeatCount:4];
    [view.layer addAnimation:layer forKey:nil];
}

UIView/UIImageView等View 弹框效果


/**
 添加简单的弹框缓冲效果

 @param allView 动画View
 */
+ (void)animationWithAlertViewWithView:(UIView*)allView{
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.2;
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [allView.layer addAnimation:animation forKey:nil];
}