我尝试使用面罩层,以便使搜索栏中实际案文领域的所有内容具有透明度(从下面的角度来看显示内容)。 由于你必须把你面罩的形形形同乌斯敦巴拉的文字场面相匹配,因此,它可以做些什么,但又会很疲软。 如果 Apple果完全改变案文领域的形式,那么你的面罩就能够打上灯,而你看看看着你不想要的那部分探条线梯度,和(或)你 won不见你想要的文字部分。 这就是说:
//first make sure you include core animation so that the compiler will know about your view s layer
#import <QuartzCore/QuartzCore.h>
//now make a mask. this is basically just a solid colored shape. When you apply the mask, anywhere where the color is solid will become transparent in your view. i used the excellent Opacity (http://likethought.com/opacity/) to generate this code, but you can do it any way you d like
@interface SearchMaskLayer : CALayer {
}
@end
@implementation SearchMaskLayer
- (void)drawInContext:(CGContextRef)context
{
CGRect imageBounds = CGRectMake(0, 0, 310, 34);
CGRect bounds = imageBounds;
CGFloat alignStroke;
CGFloat resolution;
CGMutablePathRef path;
CGPoint point;
CGPoint controlPoint1;
CGPoint controlPoint2;
UIColor *color;
resolution = 0.5 * (bounds.size.width / imageBounds.size.width + bounds.size.height / imageBounds.size.height);
CGContextSaveGState(context);
CGContextTranslateCTM(context, bounds.origin.x, bounds.origin.y);
CGContextScaleCTM(context, (bounds.size.width / imageBounds.size.width), (bounds.size.height / imageBounds.size.height));
// Layer 1
alignStroke = 0.0;
path = CGPathCreateMutable();
point = CGPointMake(295.0, 32.0);
point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
CGPathMoveToPoint(path, NULL, point.x, point.y);
point = CGPointMake(310.0, 17.0);
point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
controlPoint1 = CGPointMake(303.229, 32.0);
controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
controlPoint2 = CGPointMake(310.0, 25.229);
controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
point = CGPointMake(310.0, 17.0);
point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
CGPathAddLineToPoint(path, NULL, point.x, point.y);
point = CGPointMake(295.0, 2.0);
point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
controlPoint1 = CGPointMake(310.0, 8.771);
controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
controlPoint2 = CGPointMake(303.229, 2.0);
controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
point = CGPointMake(15.0, 2.0);
point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
CGPathAddLineToPoint(path, NULL, point.x, point.y);
point = CGPointMake(0.0, 17.0);
point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
controlPoint1 = CGPointMake(6.771, 2.0);
controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
controlPoint2 = CGPointMake(0.0, 8.771);
controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
point = CGPointMake(0.0, 17.0);
point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
CGPathAddLineToPoint(path, NULL, point.x, point.y);
point = CGPointMake(15.0, 32.0);
point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
controlPoint1 = CGPointMake(0.0, 25.229);
controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
controlPoint2 = CGPointMake(6.771, 32.0);
controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
point = CGPointMake(295.0, 32.0);
point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
CGPathAddLineToPoint(path, NULL, point.x, point.y);
CGPathCloseSubpath(path);
color = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
[color setFill];
CGContextAddPath(context, path);
CGContextFillPath(context);
CGPathRelease(path);
}
后来,在你的一种方法中,使用面罩。
- (void)viewDidLoad {
[super viewDidLoad];
SearchMaskLayer *maskLayer = [[SearchMaskLayer alloc] init];
[maskLayer setFrame:CGRectMake(0, 0, 310, 34)];
[maskLayer setPosition:CGPointMake(162,21)];
[maskLayer setNeedsDisplay];
[self.searchBar.layer setNeedsDisplay];
[self.searchBar.layer setMask:maskLayer];
[maskLayer release];
}
我认为,这个层的位置只是看它想要看什么,但你可以计算这些大小和位置,以满足你们的需要。