有時候一張圖片可能沒有要全部完整顯示,會根據不同的情形選用不同的解法
今天要介紹的是用另外張圖片來遮罩另外一張圖片
我遮罩圖用這張
圖片選用這張
效果出來會是這樣
- CALayer *maskInfo = [CALayer layer];
- mask.contents = (id)[[UIImage imageNamed:@"MASK.png"] CGImage];
- mask.frame = CGRectMake(0, 0, self.imageForMask.frame.size.width, self.imageForMask.frame.size.height);
- self.imageForMask.layer.mask = mask;
- self.imageForMask.layer.masksToBounds = YES;
只要這樣就完成啦!!!
ps. 遮罩是利用透明度去遮罩跟顏色無關,也就是會把透明度為0的去掉,保留透明度不為0的部分
額外進階
又要遮罩又要旋轉
旋轉功能詳細請看上篇網誌
- UIView *view = self.imagePhoto;
- CALayer *mask = [CALayer layer];
- mask.contents = (id)[[UIImage imageNamed:@"MASK.png"] CGImage];
- mask.frame = CGRectMake(0, 0, self. imagePhoto.frame.size.width, self. imagePhoto.frame.size.height);
- float centerX = view.bounds.size.width/2;
- float centerY = view.bounds.size.height/2;
- float x = view.bounds.size.width;
- float y = view.bounds.size.height;
- //GetCGAffineTransformRotateAroundPoint(centerX,centerY ,45.0/180.0*M_PI)
- CGAffineTransform trans = [self GetCGAffineTransformRotateAroundPoint:centerX :centerY :x :y :(-40.0f * (M_PI / 180.0f))];
- view.transform = CGAffineTransformIdentity;
- view.transform = trans;
- view.layer.allowsEdgeAntialiasing = YES;
- view = self.imageBgLeft;
- centerX = view.bounds.size.width/2;
- centerY = view.bounds.size.height/2;
- x = view.bounds.size.width;
- y = view.bounds.size.height;
- trans = [self GetCGAffineTransformRotateAroundPoint:centerX :centerY :x :y :(-40.0f * (M_PI / 180.0f))];
- view.transform = CGAffineTransformIdentity;
- view.transform = trans;
- view.layer.allowsEdgeAntialiasing = YES;
- self. imagePhoto.layer.mask = mask;
- self. imagePhoto.layer.masksToBounds = YES;
主要關鍵在於
先把frame給遮罩,要在旋轉之前,否則就會因為旋轉後frame不一樣會不好對到位置
//***************************************************
另外提一下把view縮小可以這樣使用
self.view.transform = CGAffineTransformMakeScale(0.5, 0.5);
這樣就可以縮小囉~
當然也可以放大,比例自己再去調整囉~
沒有留言:
張貼留言