8APR
在Core Animation的层中使用遮罩使图片的某些部分变为透明是非常简单的事情,你只需要建立一个遮罩层,设置为另一个层的mask即可。相关信息在本站的 Core Animation:用最少代码画出照片倒影效果 中有所提及。只是之前例子中的遮罩是使用Photoshop进行创建的。
本文通过一个简单例子,直接创建一个CGImageRef图片做为遮罩,继续讲解如何在Core Animation中使用遮罩。
本文的例子实现了以下几个方面:
- 创建一个NSImage,用NSBezIErPath填充。
- 将NSImage转换为CGImageRef (本站有相关文章讲解如何转换图片数据类型)。
- 建立三个CALayer,分别用于:显示遮罩图片、被遮罩的图片以及合并后的效果图。
作者:MatthIEu CormIEr 原文地址
相关代码如下:
- (voID)awakeFromNib {
[self maskTest];
}
- (CGImageRef)createMaskImage {
[[NScolor colorWithCalibrateDWhite:1.0 Alpha] set];
float minX = 0;
float minY = float maxX = 100;
float maxY = 100;
NSBezIErPath* path = [NSBezIErPath bezIErPath];
NSPoint topleft = NSMakePoint (minX,maxY);
NSPoint topRight = NSMakePoint (maxX,255)">NSPoint bottomright = NSPoint bottomleft =
NSPoint centerPoint = NSMakePoint((maxX - minX) * 0.5,(maxY - minY) * 0.5);
[path movetoPoint:topleft];
[path linetoPoint:topRight];
[path curvetoPoint:centerPoint controlPoint1: controlPoint2:centerPoint];
[path curvetoPoint:bottomright controlPoint2:bottomright];
[path linetoPoint:bottomleft];
NSImage* maskImage = [[NSImage alloc] initWithSize:NSMakeSize(100, )];
[maskImage lockFocus];
{
];
[path fill];
[path stroke];
}
[maskImage unlockFocus];
CGImageSourceRef source;
source = CGImageSourceCreateWithData((CFDataRef)[maskImage TIFFRepresentation], NulL);
CGImageRef maskRef = CGImageSourceCreateImageAtIndex(source,207)">0);
[maskImage release];
return maskRef;
}
- (voID)maskTest {
CGRect vIEwFrame = NSRectTocgRect( self.frame );
vIEwFrame.origin.y = 0;
// create a layer and match its frame to the vIEw’s frame
self.wantsLayer = YES;
CALayer* mainLayer = self.layer;
mainLayer.name = @H_44_404@@"mainLayer";
mainLayer.frame = vIEwFrame;
mainLayer.delegate = self;
// causes the layer content to be drawn in -drawRect:
[mainLayer setNeedsdisplay];
CGfloat mIDX = CGRectGetMIDX( mainLayer.frame );
CGfloat mIDY = CGRectGetMIDY( mainLayer.frame );
// create a "container" layer for all content layers.
// same frame as the vIEw’s master layer,automatically
// resizes as necessary.
CALayer* contentContainer = [CALayer layer];
contentContainer.bounds = mainLayer.bounds;
contentContainer.delegate = self;
contentContainer.anchorPoint = CGPointMake(0.5);
contentContainer.position = CGPointMake( mIDX,mIDY );
contentContainer.autoresizingMask = kCALayerWIDthSizable | kCALayerHeightSizable;
contentContainer.borderWIDth = 2.0;
contentContainer.bordercolor = SFWhitecolor;
[self.layer addSublayer:contentContainer];
CALayer* BoxLayer = [layer];
BoxLayer.frame = CGRectMake(0, 100,207)">100);
BoxLayer.backgroundcolor = CGcolorCreateGenericRGB0.0f1.0f);
BoxLayer.position = CGPointMake11010);
BoxLayer.anchorPoint = 0);
CALayer* maskLayer = [layer];
maskLayer.frame = 100);
maskLayer.position = 0);
maskLayer.anchorPoint = 0);
CGImageRef maskImage = [self createMaskImage];
[maskLayer setContents
ID)maskImage];
[BoxLayer setMask:maskLayer];
[contentContainer addSublayer:BoxLayer];
[contentContainer layoutSublayers];
[contentContainer layoutIfNeeded];
}
例子项目文件在此下载
总结以上是内存溢出为你收集整理的Core Animation教学:如何使用CALayer的遮罩(mask)全部内容,希望文章能够帮你解决Core Animation教学:如何使用CALayer的遮罩(mask)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)