转摘 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 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 100, 200, 30);
[button setBackgroundColor:[UIColor redColor]];
[button setTitle:@"FirstViewController" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
[animation setFromValue:[NSNumber numberWithFloat:8]];
[animation setToValue:[NSNumber numberWithFloat:1]];
[animation setDuration:5];
[animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.4 :1.3 :1 :1]];
[button.layer addAnimation:animation forKey:@"bounceAnimation"];
[button release];