我正在寻找方法来向任意UIView添加逐渐淡化或可能模糊的边框(我不知道如何命名此效果)。我不需要动画效果,我需要静态效果,例如我的UITableView边框部分透明。我做了一个例子:
所以你可以看到我正在做的事情。
任何人都可以帮助我吗?
答案 0 :(得分:16)
我找到了一个解决方案 - 我使用了CALayer的属性掩码:
CALayer *viewLayer = [back layer];
CALayer* maskCompoudLayer = [CALayer layer];
maskLayer.bounds = viewLayer.bounds;
[maskLayer setPosition:CGPointMake(160, CGRectGetHeight(maskCompoudLayer.frame)/2.0)];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate (NULL, 320, 480, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGFloat colors[] = {
0.5, 0.5, 0.5, 0.0, //BLACK
0.0, 0.0, 0.0, 1.0, //BLACK
};
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
CGColorSpaceRelease(colorSpace);
NSUInteger gradientH = 20;
NSUInteger gradientHPos = 0;
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0].CGColor);
CGContextFillRect(context, CGRectMake(0, gradientHPos + gradientH, CGRectGetWidth(maskLayer.frame), CGRectGetHeight(maskLayer.frame)));
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.0].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, 320, gradientHPos));
CGContextDrawLinearGradient(context, gradient, CGPointMake(160, gradientHPos), CGPointMake(160, gradientHPos + gradientH), 0);
CGGradientRelease(gradient);
CGImageRef contextImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
[maskLayer setContents:(id)contextImage];
CGImageRelease (contextImage);
viewLayer.masksToBounds = YES;
viewLayer.mask = maskCompoudLayer;
使用此代码,我的UITableView带有淡化边框
答案 1 :(得分:10)
我首先要说的是,iKiR的答案对我来说已经足够了。我按照原样复制了代码,并且在我身边没有多少经验的情况下,我能够毫不费力地使其工作(在UITableView
上)。
MaskingView
QuartzCore
framework!initWithCoder:
和/或initWithFrame:
视情况而定)MaskingView
中添加要应用不透明度羽化的视图。 (在Interface Builder中,用户编辑器 - >嵌入 - >查看。然后,选择新超级视图的类为MaskingView
)备注:强>
守则:
CALayer *viewLayer = [self layer];
CALayer* maskLayer = [CALayer layer];
maskLayer.bounds = viewLayer.bounds;
[maskLayer setPosition:CGPointMake(CGRectGetWidth(viewLayer.frame)/2.0, CGRectGetHeight(viewLayer.frame)/2.0)];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate (NULL, viewLayer.bounds.size.width, viewLayer.bounds.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGFloat colors[] = {
0.5, 0.5, 0.5, 0.0, //BLACK
0.0, 0.0, 0.0, 1.0, //BLACK
};
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
CGColorSpaceRelease(colorSpace);
NSUInteger gradientH = 20;
NSUInteger gradientHPos = 0;
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0].CGColor);
CGContextFillRect(context, CGRectMake(0, gradientHPos + gradientH, CGRectGetWidth(maskLayer.frame), CGRectGetHeight(maskLayer.frame)));
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.0].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, 320, gradientHPos));
CGContextDrawLinearGradient(context, gradient, CGPointMake(160, gradientHPos), CGPointMake(160, gradientHPos + gradientH), 0);
CGGradientRelease(gradient);
CGImageRef contextImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
[maskLayer setContents:(__bridge id)contextImage];
CGImageRelease (contextImage);
viewLayer.masksToBounds = YES;
viewLayer.mask = maskLayer;
答案 2 :(得分:3)
您可以尝试在UITableView的底部放置一个半透明的PNG文件。
答案 3 :(得分:3)
Cocoanetics有一个教程可以做到这一点。
答案 4 :(得分:1)
这是iKiR和Mazyod的答案,翻译成Xamarin.iOS(Monotouch)。请注意,如果只是将正确的标志传递给DrawLinearGradient
方法,则无需在渐变之前和之后绘制矩形:
var vl = myView.Layer;
var l = new CALayer ();
l.Frame = new RectangleF(vl.Frame.Width/2, vl.Frame.Height/2,
vl.Frame.Width, vl.Frame.Height) ;
var cs = CGColorSpace.CreateDeviceRGB ();
var g = new CGBitmapContext (null,
(int) vl.Bounds.Size.Width, (int)vl.Bounds.Size.Height,
8, 0, cs, CGImageAlphaInfo.PremultipliedLast);
var colors = new CGColor[] { UIColor.FromWhiteAlpha(1, 0).CGColor,
UIColor.FromWhiteAlpha(1, 1f).CGColor };
var grad = new CGGradient (cs, colors, new float[] { 0f, 1f });
int gradH = 20, gradHPos = 0;
g.DrawLinearGradient (grad,
new PointF (l.Frame.Width / 2, gradHPos), new PointF (l.Frame.Width / 2, gradHPos + gradH),
CGGradientDrawingOptions.DrawsBeforeStartLocation | CGGradientDrawingOptions.DrawsAfterEndLocation);
grad.Dispose ();
l.Contents = g.ToImage ();
g.Dispose ();
vl.Mask = l;
vl.MasksToBounds = true;