iphone - 聚焦效果(就像UIAlertView一样)

时间:2011-05-13 09:23:18

标签: iphone focus

我知道我的问题的标题是如此糟糕,但我不知道如何描述它。

当UIAlertView弹出时,屏幕上的任何其他内容(UIAlertView除外)会变得有点暗,但可以看到。我把它称为焦点效果,因为你会清楚直接地知道现在UIAlertView是焦点。

那么我该如何实现这样的焦点效果呢?

感谢

4 个答案:

答案 0 :(得分:4)

只需在要“聚焦”的视图下方添加半透明视图即可。 简单的例子:

UIView *shieldView = [[[UIView alloc] initWithFrame:myView.bounds] autorelease];
shieldView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.7];
[myView.superview insertSubview:shieldView belowSubview:myView];

UIAlertView实际上使用的是具有径向渐变而非简单颜色的图像,以突出显示视图的中心。

答案 1 :(得分:4)

我知道这篇文章有点陈旧,但我认为这可能有助于某人。

使用此代码生成径向渐变背景:

- (UIImage *)radialGradientImage:(CGSize)size start:(float)start end:(float)end centre:(CGPoint)centre radius:(float)radius{
UIGraphicsBeginImageContextWithOptions(size, YES, 1);

size_t count = 2;
CGFloat locations[2] = {0.0, 1.0};
CGFloat components[8] = {start, start, start, 1.0, end, end, end, 1.0};

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef grad = CGGradientCreateWithColorComponents (colorSpace, components, locations, count);
CGColorSpaceRelease(colorSpace);

CGContextDrawRadialGradient (UIGraphicsGetCurrentContext(), grad, centre, 0, centre, radius, kCGGradientDrawsAfterEndLocation);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

CGGradientRelease(grad);
UIGraphicsEndImageContext();
return image;}

在.h文件中定义渐变,如下所示:

UIImageView *gradient;

像这样调用渐变:

- (void)addGradient{
CGSize size = self.view.bounds.size;
CGPoint centre = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);

float startColor = 1.0f; 
float endColor = 0.0f;
float radius = MIN(self.view.bounds.size.width/4, self.view.bounds.size.height/4); 

gradient = [[UIImageView alloc] initWithImage:[self radialGradientImage:size 
                                                                  start:startColor 
                                                                    end:endColor 
                                                                 centre:centre 
                                                                 radius:radius]];

[gradient setBackgroundColor:[UIColor clearColor]];
[gradient setUserInteractionEnabled:YES];
[gradient setAlpha:0.6f];
[self.view addSubview:gradient];}

答案 2 :(得分:2)

UIAlertView就是这样的。它在alpha蒙版图像中淡入淡出背景。动画完成后,它将启动对话框的“反弹”动画。

因此,要重现它,首先需要生成一个带有“亮点”的alpha蒙版,其中对话框最终会淡入其中。然后使用(少数)帧动画来获得反弹效果。

此处有更多信息:Creating a Pop animation similar to the presentation of UIAlertView

答案 3 :(得分:0)

为了使它比“不好”更好,你可以......

  1. 在笔尖中创建一个UIView(如果您需要效果的部分代码已经使用了笔尖,则最简单),然后将半透明图形(具有“焦点”效果)添加到该视图中。 / p>

  2. 将笔尖中的UIView连接到IBOutlet

  3. 使用动画将图形淡入视图层次结构(omz示例显示)