少见的几种动画效果

来源:互联网 发布:vue2.0源码分析 编辑:程序博客网 时间:2024/06/08 06:26

  转摘 http://marshal.easymorse.com/archives/3727,自己调试通过的代码

包含

   #import <QuartzCore/QuartzCore.h>


- (void)loadView
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
    UIImage *image=[UIImage imageNamed:@"back0.jpg"];
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, 768, 1024, 8, 4 * 768, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGRect rect = CGRectMake(0, 0, 768, 1024);
    CGColorRef fillColor = [[UIColor whiteColor] CGColor];
    CGContextSetFillColor(context, CGColorGetComponents(fillColor));
    
    CGContextMoveToPoint(context, 160.0f, 230.0f);
    CGContextAddLineToPoint(context, 600.0f, 230.0f);
    CGContextAddLineToPoint(context, 600.0f, 100.0f);
    CGContextAddLineToPoint(context, 370.0f, 50.0f);
    CGContextAddLineToPoint(context, 200.0f, 100.0f);
    
    CGContextClosePath(context);
    CGContextClip(context);
    CGContextDrawImage(context, rect, image.CGImage);
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    UIImage *newImage = [UIImage imageWithCGImage:imageMasked];
    CGImageRelease(imageMasked);
    
    UIImageView *backView=[[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    [self.view addSubview:backView];
    backView.image=newImage;
    backView.alpha=0.3;
    
    CABasicAnimation *theAnimation1;    //定义动画
    /*
    //左右摇摆
    theAnimation1=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    theAnimation1.fromValue=[NSNumber numberWithFloat:0];
    theAnimation1.toValue=[NSNumber numberWithFloat:-100];
    
    theAnimation1.duration=5.5;//动画持续时间
    theAnimation1.repeatCount=6;//动画重复次数
    theAnimation1.autoreverses=YES;//是否自动重复*/
    
    //旋转
    //theAnimation1=[CABasicAnimation animationWithKeyPath:@"transform"];
    //theAnimation1.toValue = [ NSValue valueWithCATransform3D: CATransform3DMakeRotation(3.1415, 0, 0, 1.0) ];
    
    //缩放
    theAnimation1=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
    theAnimation1.toValue = [NSNumber numberWithDouble:2.5];
    
    
    [backView.layer addAnimation:theAnimation1 forKey:@"animateLayer"];
    
    [newImage release];
    [image release];
}


//按钮从大变小,然后小于正常显示的值,最后恢复正常(就是缩小并有回弹的效果)

 UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    button.frame = CGRectMake(20,100, 200, 30);

    [button setBackgroundColor:[UIColorredColor]];

    [button setTitle:@"FirstViewController"forState:UIControlStateNormal];

    [button addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

    CABasicAnimation * animation = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];

    [animation setFromValue:[NSNumbernumberWithFloat:8]];

    [animation setToValue:[NSNumbernumberWithFloat:1]];

    [animation setDuration:5];

    [animation setTimingFunction:[CAMediaTimingFunctionfunctionWithControlPoints:.4 :1.3 :1 :1]];

    [button.layer addAnimation:animationforKey:@"bounceAnimation"];

    

    [button release];