UIView图标动画

时间:2016-11-15 06:22:16

标签: ios objective-c uiview uiimage core-animation

我试图在用户点击视图时进行爆破动画。当用户单击特定视图时,我将视图破解成圆形片段。所以我已经将uiview转换为uiimage,如下所示,

   - (UIImage *) imageWithView:(UIView *)view
    {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];

        UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return img;
    }

然后我将uiimage打成碎片如下,

-(void)splitImage:(UIImage *)image
{
    CGFloat imgWidth = image.size.width/2;
    CGFloat imgheight = image.size.height;
    CGRect leftImgFrame = CGRectMake(0, 0, imgWidth, imgheight);
    CGRect rightImgFrame = CGRectMake(imgWidth, 0, imgWidth, imgheight);

    CGImageRef left = CGImageCreateWithImageInRect(image.CGImage, leftImgFrame);
    CGImageRef right = CGImageCreateWithImageInRect(image.CGImage, rightImgFrame);

    UIImage* leftImage = [UIImage imageWithCGImage:left];
    UIImage* rightImage = [UIImage imageWithCGImage:right];

    CGImageRelease(left);
    CGImageRelease(right);
}

但是在做这件事时我会遇到一些问题。

  1. 我能够将uiimage打破成两件而不是 动态的作品。
  2. 我怎么能表现出像uiview那样用破碎的uiimages迸发成圆形碎片?
  3. 更新 以下是我更新的代码...

    -(void)startAnimation{
    
    
        //Add the initial circle
    //    UIView* circleView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 60, 60)];
        UIView *circleView = [[UIImageView alloc] initWithFrame:self.submit.bounds];
    
        circleView.bounds = self.submit.bounds;
    
        CAShapeLayer *circleLayer = [CAShapeLayer layer];
    
        //set colors
        [circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
        [circleLayer setFillColor:[[UIColor clearColor] CGColor]];
        [circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:circleView.bounds] CGPath]];
        [circleView.layer addSublayer:circleLayer];
        [self.view addSubview:circleView];
    
        //Animate circle
        [circleView setTransform:CGAffineTransformMakeScale(0, 0)];
        [UIView animateWithDuration:0.7 animations:^{
            [circleView setTransform:CGAffineTransformMakeScale(1.3, 1.3)];
        } completion:^(BOOL finished) {
            circleView.hidden = YES;
            //start next animation
            [self createIconAnimation];
        }];
    }
    
    -(void)createIconAnimation{
    
        //load icon which pops up
        UIImageView* iconImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_tick"]];
        iconImage.frame = CGRectMake(50, 50, 60, 60);
        iconImage.bounds = self.submit.bounds;
        [iconImage setTransform:CGAffineTransformMakeScale(0, 0)];
        [self.view addSubview:iconImage];
    
        //animate icon
        [UIView animateWithDuration:0.3/1.5 animations:^{
            iconImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3/2 animations:^{
                iconImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.3/2 animations:^{
                    iconImage.transform = CGAffineTransformIdentity;
                }];
            }];
        }];
    
    
        //add circles around the icon
        int numberOfCircles = 20;
        CGPoint center = iconImage.center;
        float radius= 35;
        BOOL isBig = YES;;
        for (int i = 0; i<numberOfCircles; i++) {
    
            float x = radius*cos(M_PI/numberOfCircles*i*2) + center.x;
            float y = radius*sin(M_PI/numberOfCircles*i*2) + center.y;
    
            float circleRadius = 10;
            if (isBig) {
                circleRadius = 5;
                isBig = NO;
            }else{
                isBig = YES;
            }
    
            UIView* circleView = [[UIView alloc] initWithFrame:CGRectMake(x, y, circleRadius, circleRadius)];
            CAShapeLayer *circleLayer = [CAShapeLayer layer];
            [circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
            [circleLayer setFillColor:[[UIColor redColor] CGColor]];
            [circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:circleView.bounds] CGPath]];
            [circleView.layer addSublayer:circleLayer];
            [self.view addSubview:circleView];
    
            //animate circles
            [UIView animateWithDuration:0.8 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                [circleView setTransform:CGAffineTransformMakeTranslation(radius/3*cos(M_PI/numberOfCircles*i*2), radius/3*sin(M_PI/numberOfCircles*i*2))];
                [circleView setTransform:CGAffineTransformScale(circleView.transform, 0.01, 0.01)];
            } completion:^(BOOL finished) {
                circleView.hidden = YES;
            }];
    
    
        }
    
    }
    

    动画必须位于self.submit按钮的顶部,但它不位于其上方

1 个答案:

答案 0 :(得分:1)

在这里,您只需将此代码添加到视图控制器即可。它给你这个结果:

http://imgur.com/a/PhcIv

只需使用颜色和动画来获得所需的效果。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self startAnimation];

}

-(void)startAnimation{

    //Add the initial circle
    UIView* circleView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 60, 60)];
    CAShapeLayer *circleLayer = [CAShapeLayer layer];

    //set colors
    [circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
    [circleLayer setFillColor:[[UIColor clearColor] CGColor]];
    [circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:circleView.bounds] CGPath]];
    [circleView.layer addSublayer:circleLayer];
    [self.view addSubview:circleView];

    //Animate circle
    [circleView setTransform:CGAffineTransformMakeScale(0, 0)];
    [UIView animateWithDuration:0.7 animations:^{
        [circleView setTransform:CGAffineTransformMakeScale(1.3, 1.3)];
    } completion:^(BOOL finished) {
        circleView.hidden = YES;
        //start next animation
        [self createIconAnimation];
    }];
}

-(void)createIconAnimation{

    //load icon which pops up
    UIImageView* iconImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Untitled"]];
    iconImage.frame = CGRectMake(50, 50, 60, 60);
    [iconImage setTransform:CGAffineTransformMakeScale(0, 0)];
    [self.view addSubview:iconImage];

    //animate icon
    [UIView animateWithDuration:0.3/1.5 animations:^{
        iconImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3/2 animations:^{
            iconImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3/2 animations:^{
                iconImage.transform = CGAffineTransformIdentity;
            }];
        }];
    }];


    //add circles around the icon
    int numberOfCircles = 20;
    CGPoint center = iconImage.center;
    float radius= 35;
    BOOL isBig = YES;;
    for (int i = 0; i<numberOfCircles; i++) {

        float x = radius*cos(M_PI/numberOfCircles*i*2) + center.x;
        float y = radius*sin(M_PI/numberOfCircles*i*2) + center.y;

        float circleRadius = 10;
        if (isBig) {
            circleRadius = 5;
            isBig = NO;
        }else{
            isBig = YES;
        }

        UIView* circleView = [[UIView alloc] initWithFrame:CGRectMake(x, y, circleRadius, circleRadius)];
        CAShapeLayer *circleLayer = [CAShapeLayer layer];
        [circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
        [circleLayer setFillColor:[[UIColor redColor] CGColor]];
        [circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:circleView.bounds] CGPath]];
        [circleView.layer addSublayer:circleLayer];
        [self.view addSubview:circleView];

        //animate circles
        [UIView animateWithDuration:0.8 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            [circleView setTransform:CGAffineTransformMakeTranslation(radius/3*cos(M_PI/numberOfCircles*i*2), radius/3*sin(M_PI/numberOfCircles*i*2))];
            [circleView setTransform:CGAffineTransformScale(circleView.transform, 0.01, 0.01)];
        } completion:^(BOOL finished) {
            circleView.hidden = YES;
        }];


    }

}



@end