我希望你知道类别?
创建一个类别将是更好的选择:
Command + N > Objective-C Category > Category = Animation & Category on = UIView
这将创建 2 个文件,其名称UIView+Animation.h
为UIView+Animation.m
UIView+Animation.h
文件
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface UIView (Animation)
- (void)setBackgroundShadow:(UIColor *)shadowColor CGSize:(CGSize)CGSize shadowOpacity:(float)shadowOpacity shadowRadius:(float)shadowRadius;
@end
UIView+Animation.m
文件
#import "UIView+Animation.h"
@implementation UIView (Animation)
- (void)setBackgroundShadow:(UIColor *)shadowColor CGSize:(CGSize)CGSize shadowOpacity:(float)shadowOpacity shadowRadius:(float)shadowRadius
{
self.layer.shadowColor = shadowColor.CGColor;
self.layer.shadowOffset = CGSize;
self.layer.shadowOpacity = shadowOpacity;
self.layer.shadowRadius = shadowRadius;
self.clipsToBounds = NO;
}
导入UIView+Animation.h
您的任何一个viewController
并像这样调用它:
[self.titleLabel setBackgroundShadow:[UIColor grayColor] CGSize:CGSizeMake(0, 5) shadowOpacity:1 shadowRadius:5.0];