ios 7 下自定义alertView
项目需要适配ios7,在启动页面原来ios6可以运行的alertView上加addsubView已经不起作用,所以决定自己写一个alertView。思路也就是自己做模态蒙板半透明的,给view做show,和close的动画。下面上代码,动画部分是直接取自己code4上面的。
点击按钮
#import <QuartzCore/QuartzCore.h>
- (void)show{ self.STAlertView.center = self.view.center; [self.view addSubview:self.STAlertView]; CAKeyframeAnimation *popAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; popAnimation.duration = 0.4; popAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)], [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)], [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)], [NSValue valueWithCATransform3D:CATransform3DIdentity]]; popAnimation.keyTimes = @[@0.2f, @0.5f, @0.75f, @1.0f]; popAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; [self.STAlertView.layer addAnimation:popAnimation forKey:nil]; }
背景加上的是黑色半透明的view,注意不要直接调view的alpha
UIView *maskView=[[UIView alloc] initWithFrame:self.view.bounds]; maskView.backgroundColor=[UIColor colorWithRed:40/255.0 green:43/255.0 blue:52/255.0 alpha:0.6]; [self.view addSubview:maskView]; [maskView release];
隐藏动画部分,动画下去之后移除alertView
- (IBAction)hideAlertAction:(id)sender { CAKeyframeAnimation *hideAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; hideAnimation.duration = 0.4; hideAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)], [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0f, 1.0f, 1.0f)], [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.00f, 0.00f, 0.00f)]]; hideAnimation.keyTimes = @[@0.2f, @0.5f, @0.75f]; hideAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; hideAnimation.delegate = self; [self.STAlertView.layer addAnimation:hideAnimation forKey:nil]; } - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { [self.STAlertView removeFromSuperview]; }
在这自定义这个alertView的部分没有做高强度测试,可以自己慢慢优化