iOS开发中常用类目
来源:互联网 发布:catia v6软件下载 编辑:程序博客网 时间:2024/05/17 23:49
1.NSDateFomatter
Objective-C
1
2
3
4
5
6
7
8
@interface NSDateFormatter (MyCategory)
+ (id)dateFormatter;
+ (id)dateFormatterWithFormat:(NSString *)dateFormat;
+ (id)defaultDateFormatter;
@end
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@implementationNSDateFormatter(MyCategory)
+(id)dateFormatter
{
return[[selfalloc]init];
}
+(id)dateFormatterWithFormat:(NSString*)dateFormat
{
NSDateFormatter*dateFormatter=[[selfalloc]init];
dateFormatter.dateFormat=dateFormat;
returndateFormatter;
}
+(id)defaultDateFormatter
{
return[selfdateFormatterWithFormat:@"yyyy-MM-dd HH:mm:ss"];
}
@end
2.NSDate
Objective-C
1
2
3
4
5
6
7
@interface NSDate (MyCategory)
- (NSString *)timeIntervalDescription;//距离当前的时间间隔
- (NSString *)minuteDescription;//精确到分钟的日期
- (NSString *)formattedDateDescription;//格式化日期
@end
Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@implementationNSDate(MyCategory)
-(NSString*)timeIntervalDescription
{
NSTimeIntervaltimeInterval=-[selftimeIntervalSinceNow];
if(timeInterval<60){
return@"1分钟内";
}elseif(timeInterval<3600){
return[NSString stringWithFormat:@"%.f分钟前",timeInterval/60];
}elseif(timeInterval<86400){
return[NSString stringWithFormat:@"%.f小时前",timeInterval/3600];
}elseif(timeInterval<2592000){//30天内
return[NSString stringWithFormat:@"%.f天前",timeInterval/86400];
}elseif(timeInterval<31536000){//30天至1年内
NSDateFormatter*dateFormatter=[NSDateFormatter dateFormatterWithFormat:@"M月d日"];
return[dateFormatter stringFromDate:self];
}else{
return[NSString stringWithFormat:@"%.f年前",timeInterval/31536000];
}
}
-(NSString*)minuteDescription
{
NSDateFormatter*dateFormatter=[NSDateFormatter dateFormatterWithFormat:@"yyyy-MM-dd"];
NSString*theDay=[dateFormatter stringFromDate:self];//日期的年月日
NSString*currentDay=[dateFormatter stringFromDate:[NSDatedate]];//当前年月日
if([theDay isEqualToString:currentDay]){//当天
[dateFormatter setDateFormat:@"ah:mm"];
return[dateFormatter stringFromDate:self];
}elseif([[dateFormatter dateFromString:currentDay] timeIntervalSinceDate:[dateFormatter dateFromString:theDay]]==86400){//昨天
[dateFormatter setDateFormat:@"ah:mm"];
return[NSString stringWithFormat:@"昨天 %@",[dateFormatter stringFromDate:self]];
}elseif([[dateFormatter dateFromString:currentDay] timeIntervalSinceDate:[dateFormatter dateFromString:theDay]]<86400*7){//间隔一周内
[dateFormatter setDateFormat:@"EEEE ah:mm"];
return[dateFormatter stringFromDate:self];
}else{//以前
[dateFormatter setDateFormat:@"yyyy-MM-dd ah:mm"];
return[dateFormatter stringFromDate:self];
}
}
-(NSString*)formattedDateDescription
{
NSDateFormatter*dateFormatter=[[NSDateFormatteralloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString*theDay=[dateFormatter stringFromDate:self];//日期的年月日
NSString*currentDay=[dateFormatter stringFromDate:[NSDatedate]];//当前年月日
NSIntegertimeInterval=-[selftimeIntervalSinceNow];
if(timeInterval<60){
return@"1分钟内";
}elseif(timeInterval<3600){//1小时内
return[NSString stringWithFormat:@"%d分钟前",timeInterval/60];
}elseif(timeInterval<21600){//6小时内
return[NSString stringWithFormat:@"%d小时前",timeInterval/3600];
}elseif([theDay isEqualToString:currentDay]){//当天
[dateFormatter setDateFormat:@"HH:mm"];
return[NSString stringWithFormat:@"今天 %@",[dateFormatter stringFromDate:self]];
}elseif([[dateFormatter dateFromString:currentDay] timeIntervalSinceDate:[dateFormatter dateFromString:theDay]]==86400){//昨天
[dateFormatter setDateFormat:@"HH:mm"];
return[NSString stringWithFormat:@"昨天 %@",[dateFormatter stringFromDate:self]];
}else{//以前
[dateFormatter setDateFormat:@"MM-dd HH:mm"];
return[dateFormatter stringFromDate:self];
}
}
@end
3.UIColor
Objective-C
1
2
3
4
5
@interface UIColor (MyCategory)
+ (UIColor *)colorWithHexRGB:(NSString *)hexRGBString;
@end
Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@implementationUIColor(MyCategory)
/*由16进制字符串获取颜色*/
+(UIColor*)colorWithHexRGB:(NSString*)hexRGBString
{
if([hexRGBString hasPrefix:@"#"]){
hexRGBString=[hexRGBString substringFromIndex:1];
}
unsignedintcolorCode=0;
unsignedcharredByte,greenByte,blueByte;
if(hexRGBString){
NSScanner*scanner=[NSScanner scannerWithString:hexRGBString];
[scanner scanHexInt:&colorCode];
}
redByte=(unsignedchar)(colorCode>>16);
greenByte=(unsignedchar)(colorCode>>8);
blueByte=(unsignedchar)(colorCode);// masks off high bits
return[UIColor colorWithRed:(float)redByte/0xff green:(float)greenByte/0xff blue:(float)blueByte/0xff alpha:1.0];
}
@end
4.UIImage
Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
@interface UIImage (MyCategory)
//图片拉伸、平铺接口
- (UIImage *)resizableImageWithCompatibleCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
//图片以ScaleToFit方式拉伸后的CGSize
- (CGSize)sizeOfScaleToFit:(CGSize)scaledSize;
//将图片转向调整为向上
- (UIImage *)fixOrientation;
//以ScaleToFit方式压缩图片
- (UIImage *)compressedImageWithSize:(CGSize)compressedSize;
@end
Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@implementationUIImage(MyCategory)
/*获取当前主题包的指定图片*/
+(UIImage*)themeImageNamed:(NSString*)name
{
NSUIntegerthemeID=[[ViewManagerdefaultManager] themeID];
if(themeID==0){//默认主题
NSString*fileName=[NSString stringWithFormat:@"Lianxi.bundle/Images/%@",name];
return[self imageNamed:fileName];
}else{//下载主题
returnnil;
}
}
/*图片拉伸、平铺接口,兼容iOS5+*/
-(UIImage*)resizableImageWithCompatibleCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
{
CGFloatversion=[[[UIDevicecurrentDevice] systemVersion] floatValue];
if(version>=6.0){
return[self resizableImageWithCapInsets:capInsets resizingMode:resizingMode];
}elseif(version>=5.0){
if(resizingMode==UIImageResizingModeStretch){
return[self stretchableImageWithLeftCapWidth:capInsets.left topCapHeight:capInsets.top];
}else{//UIImageResizingModeTile
return[self resizableImageWithCapInsets:capInsets];
}
}else{
return[self stretchableImageWithLeftCapWidth:capInsets.left topCapHeight:capInsets.top];
}
}
/*图片以ScaleToFit方式拉伸后的CGSize*/
-(CGSize)sizeOfScaleToFit:(CGSize)scaledSize
{
CGFloatscaleFactor=scaledSize.width/scaledSize.height;
CGFloatimageFactor=self.size.width/self.size.height;
if(scaleFactor<=imageFactor){//图片横向填充
returnCGSizeMake(scaledSize.width,scaledSize.width/imageFactor);
}else{//纵向填充
returnCGSizeMake(scaledSize.height*imageFactor,scaledSize.height);
}
}
/*将图片转向调整为向上*/
-(UIImage*)fixOrientation
{
if(self.imageOrientation==UIImageOrientationUp){
returnself;
}
UIGraphicsBeginImageContextWithOptions(self.size,NO,self.scale);
[self drawInRect:CGRectMake(0.0,0.0,self.size.width,self.size.height)];
UIImage*fixedImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnfixedImage;
}
/*以ScaleToFit方式压缩图片*/
-(UIImage*)compressedImageWithSize:(CGSize)compressedSize
{
if(CGSizeEqualToSize(self.size,CGSizeZero)||(self.size.width<=compressedSize.width&&self.size.height<=compressedSize.height)){//不用压缩
returnself;
}
CGSizescaledSize=[self sizeOfScaleToFit:compressedSize];
//压缩大小,调整转向
UIGraphicsBeginImageContext(scaledSize);
[self drawInRect:CGRectMake(0.0,0.0,scaledSize.width,scaledSize.height)];
UIImage*compressedImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returncompressedImage;
}
@end
5.UIView
Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@interface UIView (MyCategory)
- (void)startShakeAnimation;//摇动动画
- (void)stopShakeAnimation;
- (void)startRotateAnimation;//360°旋转动画
- (void)stopRotateAnimation;
///截图
- (UIImage *)screenshot;
@property (nonatomic) float top;
@property (nonatomic) float bottom;
@property (nonatomic) float left;
@property (nonatomic) float right;
@end
Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@implementationUIView(MyCategory)
/*截图*/
-(UIImage*)screenshot
{
UIGraphicsBeginImageContextWithOptions(self.frame.size,NO,2.0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnimage;
}
#pragma mark - Animation
-(void)startShakeAnimation
{
CGFloatrotation=0.05;
CABasicAnimation*shake=[CABasicAnimation animationWithKeyPath:@"transform"];
shake.duration=0.2;
shake.autoreverses=YES;
shake.repeatCount=MAXFLOAT;
shake.removedOnCompletion=NO;
shake.fromValue=[NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform,-rotation,0.0,0.0,1.0)];
shake.toValue =[NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform, rotation,0.0,0.0,1.0)];
[self.layer addAnimation:shake forKey:@"shakeAnimation"];
}
-(void)stopShakeAnimation
{
[self.layer removeAnimationForKey:@"shakeAnimation"];
}
-(void)startRotateAnimation
{
CABasicAnimation*shake=[CABasicAnimation animationWithKeyPath:@"transform"];
shake.duration=0.5;
shake.autoreverses=NO;
shake.repeatCount=MAXFLOAT;
shake.removedOnCompletion=NO;
shake.fromValue=[NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform,M_PI,0.0,0.0,1.0)];
shake.toValue =[NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform, 0.0,0.0,0.0,1.0)];
[self.layer addAnimation:shake forKey:@"rotateAnimation"];
}
-(void)stopRotateAnimation
{
[self.layer removeAnimationForKey:@"rotateAnimation"];
}
-(float)top
{
returnself.frame.origin.x;
}
-(float)bottom
{
returnself.top+self.frame.size.height;
}
-(float)left
{
returnself.frame.origin.x;
}
-(float)right
{
returnself.left+self.frame.size.width;
}
@end
0 0
- iOS开发中常用类目
- iOS开发中常用类目
- iOS常用类目
- iOS开发-类目、延展、协议
- IOS开发之类目,延展,协议
- iOS开发-Day21-OC ARC&类目
- iOS开发之 类目,延展,协议
- IOS开发之类目和延展
- iOS开发--创建个类目Category吧
- IOS类目
- iOS类目
- IOS类目
- iOS类目
- 关于iOS中类目的使用
- iOS开发中常用代码
- iOS开发中常用分类
- IOS 开发中常用方法
- 5.1iOS开发视频教程— 类目的基本概念和用法
- LeetCode-Evaluate Reverse Polish Notation
- 在eclipse里卸载已安装的插件
- Building a higher-level query API: the right way to use Django's ORM
- 动画编辑器和动画引擎的不同
- WP
- iOS开发中常用类目
- linux环境下的网络监听程序
- Android -- DrawerLayout
- 多线程之Mutex(互斥量)
- Python性能鸡汤
- QTextCodec中的setCodecForTr等终于消失了 (Qt5)
- FPGA DDR2接口设计
- 时代产物Hadoop
- 字符串的经典hash算法