如何淡化 UIImageView 的角/边缘/边框 [英] How do I fade the corners/edges/borders of a UIImageView
问题描述
我在论坛上找不到这个,所以我决定发布这个.
I was unable to find this on the forums so I decided to post this.
我有一个带有以下代码的 UIImageView,可以使图像彼此淡出.
I have a UIImageView with the following code which fades the images from one another.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
imageNames = [NSArray arrayWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg", @"image4.jpg", @"image5.jpg", nil];
imageview.image = [UIImage imageNamed:[imageNames objectAtIndex:0]];
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(transitionPhotos) userInfo:nil repeats:YES];
// Rounds corners of imageview
CALayer *layer = [imageview layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:5];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)transitionPhotos{
if (photoCount < [imageNames count] - 1){
photoCount ++;
}else{
photoCount = 0;
}
[UIView transitionWithView:imageview
duration:2.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{imageview.image = [UIImage imageNamed:[imageNames objectAtIndex:photoCount]]; }
completion:NULL];
}
我想实现一个代码来淡化我的图像视图的边框.
I want to implement a code that will fade the borders of my imageview.
我知道我可以使用 QuartzCore.framework,但是我找不到如何实现我想要的结果的指南.
I know I can use the QuartzCore.framework, however I was unable to find a guide how to achieve the result that I want.
执行这种方法的最短代码是什么?
What would be the shortest possible code to do such a method?
推荐答案
有一个包含蒙版的图像(透明背景上的模糊矩形):
Have an image containing a mask (blurred rectangle on transparent background):
UIImage *mask = [UIImage imageNamed:@"mask.png"];
创建一个遮罩层:
CALayer *maskLayer = [CALayer layer];
maskLayer.contents = (id)mask.CGImage;
maskLayer.frame = imageView.bounds;
并将其指定为 imageView 的掩码:
and assign it as mask for your imageView:
imageView.layer.mask = maskLayer;
您可以使用 CALayer
的阴影属性完全通过代码创建遮罩.只需使用 shadowRadius
和 shadowPath
:
you can create the mask completely by code using the shadow properties of CALayer
. Just play around with shadowRadius
and shadowPath
:
CALayer *maskLayer = [CALayer layer];
maskLayer.frame = imageView.bounds;
maskLayer.shadowRadius = 5;
maskLayer.shadowPath = CGPathCreateWithRoundedRect(CGRectInset(imageView.bounds, 5, 5), 10, 10, nil);
maskLayer.shadowOpacity = 1;
maskLayer.shadowOffset = CGSizeZero;
maskLayer.shadowColor = [UIColor whiteColor].CGColor;
imageView.layer.mask = maskLayer;
这篇关于如何淡化 UIImageView 的角/边缘/边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!