对于类似iOS 7 +的警报视图,我有这个相当不错的自定义警报视图。 Here是源教程。我遇到的唯一问题是添加的影子子视图似乎在iOS 7中不起作用。该问题的根源是在下载的示例项目中,影子添加到UIPresentationController
子类中,这样做在iOS 7中不存在,所以我尝试将其移动到动画块中,但是当动画开始时我没有看到视图出现。
这是在iOS 8版本中调用的代码中添加阴影视图的地方:
-(void)presentationTransitionWillBegin
{
[self decorateView:self.presentedView];
UIViewController *vc = self.presentingViewController;
UIView *v = vc.view;
UIView *con = self.containerView;
UIView *shadow = [[UIView alloc] initWithFrame:con.bounds];
shadow.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
shadow.alpha = 0;
[con insertSubview:shadow atIndex:0];
shadow.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[vc.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
shadow.alpha = 1;
}
completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
v.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
}];
}
以下是我尝试在iOS 7代码中执行此操作的地方:
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController* vc1 = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController* vc2 = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView* con = [transitionContext containerView];
UIView* v1 = vc1.view;
UIView* v2 = vc2.view;
// presenting
[con addSubview:v2];
UIView *shadow = [[UIView alloc] initWithFrame:con.bounds];
shadow.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
shadow.alpha = 0;
[con insertSubview:shadow atIndex:0];
shadow.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
CGAffineTransform scale = CGAffineTransformMakeScale(1.6, 1.6);
v2.transform = scale;
if ( v1 != nil )
{
v2.center = v1.center;
}
v2.alpha = 0;
[UIView animateWithDuration:0.25 animations:^
{
shadow.alpha = 1;
v2.alpha = 1;
v2.transform = CGAffineTransformIdentity;
}
completion:^(BOOL finished)
{
NSLog(@"v2 frame: %@", NSStringFromCGRect(v2.frame));
[transitionContext completeTransition:YES];
}];
以下是差异的几个截图
iOS 7:
iOS 8: