What is the best way to create a shadow behind a UIImageView(在 UIImageView 后面创建阴影的最佳方法是什么)
问题描述
我有一个 UIImageView,我想在后面添加一个阴影.我希望苹果有这个属性,但他们不得不让我们程序员做很多事情,所以我需要问这个问题.
I have a UIImageView that I want to add a shadow behind. I wish that apple had that as a property but they have to make lots of things hard for us programmers so I need to ask this question.
推荐答案
有一种更好、更简单的方法可以做到这一点.UIImageView 继承自 UIView 所以它有一个 layer 属性.您可以访问图层的阴影属性和 bam,您得到了一个阴影.
There's a better and easier way to do this. UIImageView inherits from UIView so it has a layer property. You can access the layer's shadow properties and bam, you got a shadow.
如果您将 UIImageView 作为 IBOutlet 指向 nib 文件,则只需实现 awakeFromNib例如
If you have the UIImageView as an IBOutlet to a nib file, you can just implement the awakeFromNib e.g.
Objective-C
- (void)awakeFromNib {
    imageView.layer.shadowColor = [UIColor purpleColor].CGColor;
    imageView.layer.shadowOffset = CGSizeMake(0, 1);
    imageView.layer.shadowOpacity = 1;
    imageView.layer.shadowRadius = 1.0;
    imageView.clipsToBounds = NO;
}
别忘了#import "QuartzCore/CALayer.h" 
对于 Swift,您可以采取多种方式.创建类扩展、子类或 imageView 实例.无论哪种方式,修改layers shadow属性的过程都是一样的.
For Swift, you can go about it multiple ways. Create a class extension, subclass, or an imageView instance. Whichever the way, the process is the same in modifying the layers shadow property.
斯威夫特 3
override func awakeFromNib() {
    super.awakeFromNib()
    imageView.layer.shadowColor = UIColor.purple.cgColor
    imageView.layer.shadowOffset = CGSize(width: 0, height: 1)
    imageView.layer.shadowOpacity = 1
    imageView.layer.shadowRadius = 1.0
    imageView.clipsToBounds = false
}
                        这篇关于在 UIImageView 后面创建阴影的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 UIImageView 后面创建阴影的最佳方法是什么
				
        
 
            
        基础教程推荐
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
 - iOS - UINavigationController 添加多个正确的项目? 2022-01-01
 - 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
 - 如何比较两个 NSDate:哪个是最近的? 2022-01-01
 - UIImage 在开始时不适合 UIScrollView 2022-01-01
 - 如何将图像从一项活动发送到另一项活动? 2022-01-01
 - Android Volley - 如何动画图像加载? 2022-01-01
 - Play 商店的设备兼容性问题 2022-01-01
 - SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
 - Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				