在事件上制作动画图像

时间:2014-09-04 05:08:00

标签: ios animation uiimageview

我的圆圈里面有一个大约10个圆圈的图像,每个不同的颜色我想在按钮点击从中心到外部逐个显示它们,另一个按钮点击从外到内依次消失。这可能吗?怎么样? 请建议

1 个答案:

答案 0 :(得分:1)

你可以从下面的代码中获得帮助,我没错,你正在寻找雷达风格的动画

#import "RadarAnimation.h"

@implementation RadarAnimation

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        [self SetUPAnimation];

        // Initialization code
    }
    return self;
}


-(void)SetUPAnimation
{


    [self AddRader];
    double delayInSeconds = 1.5;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self AddRader];
    });


}

-(void)AddRader
{
    UIView *view = [[UIView alloc] init];
    //    view.backgroundColor = [UIColor blueColor];
    view.frame=self.frame;
    [self setWidth:[Helper currentWidth] ofView:view];
    [self setHeight:[Helper currentWidth] ofView:view];
    [self setXPossitionto:self.frame.size.width/2-view.frame.size.width/2 ofView:view];
    [self setYPossitionto:self.frame.size.height/2-view.frame.size.height/2 ofView:view];


    view.layer.cornerRadius = CGRectGetHeight(view.frame)/2;
    view.tag=175751;
    int kRingExpansionTime=6.0;

    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.duration = kRingExpansionTime;
    scaleAnimation.repeatCount = HUGE_VAL;
    scaleAnimation.autoreverses = YES;
    scaleAnimation.fromValue = [NSNumber numberWithFloat:0.0];
    scaleAnimation.toValue = [NSNumber numberWithFloat:1.8];

    [view.layer addAnimation:scaleAnimation forKey:@"scale"];


    // fade out the ring part way thru the animation
    CABasicAnimation* alphaAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    alphaAnim.fromValue   = [NSNumber numberWithFloat:0.3];
    alphaAnim.toValue     = [NSNumber numberWithFloat:0.1];
    alphaAnim.beginTime   = kRingExpansionTime * 0.7f;      // start part way thru
    alphaAnim.duration    = kRingExpansionTime - alphaAnim.beginTime;

    CAAnimationGroup* group = [CAAnimationGroup animation];
    group.duration    = kRingExpansionTime;
    group.repeatCount = HUGE_VALF;      // repeat forever
    group.animations  = [NSArray arrayWithObjects:scaleAnimation,alphaAnim, nil];

    [view.layer addAnimation:group forKey:nil];

    view.backgroundColor = [UIColor colorWithRed:0/255.0 green:135.0/255.0 blue:219.0/255.0 alpha:0.5];
    view.alpha=0.4;
    view.layer.borderColor = [UIColor colorWithRed:0/255.0 green:135.0/255.0 blue:255.0/255.0 alpha:1.0].CGColor;
    view.layer.borderWidth = 2.0;
    //view.center=_VwEmptyplaceholder.center;
    //view.center=CGPointMake(_VwEmptyplaceholder.frame.size.width/2, _VwEmptyplaceholder.frame.size.height/2);
    view.userInteractionEnabled=NO;
    view.tag=175751;

    //[self insertSubview:view belowSubview:btn];
}
-(void)setXPossitionto:(CGFloat)xpostion ofView:(UIView *)view
{
    CGRect frame =  view.frame;
    frame.origin.x =  xpostion;
    view.frame=frame;
}

-(void)setYPossitionto:(CGFloat)ypostion ofView:(UIView *)view
{
    CGRect frame =  view.frame;
    frame.origin.y =  ypostion;
    view.frame=frame;
}

-(void)setWidth:(CGFloat)Width ofView:(UIView *)view
{
    CGRect frame =  view.frame;
    frame.size.width =  Width;
    view.frame=frame;
}

-(void)setHeight:(CGFloat)Height ofView:(UIView *)view
{
    CGRect frame =  view.frame;
    frame.size.height =  Height;
    view.frame=frame;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end