1、Category(类别)
-
什么是Category
1、Category可以在不获悉不改变原代码的情况下向已有的类中添加方法,从而达到扩展已有类的目的,但是只能添加方法,不建议删除和修改(会导致bug)。
2、无法向Category中添加实例变量,Category通常作为一种组织框架代码的工具来使用。
3、如果Category和原始类中的方法名称冲突,则Category将覆盖原始类的方法,因为Category具有更高的优先级。
-
Category的作用:
1、将类的实现分散到多个不同文件或不同框架中。
2、创建对私有方法的前向引用。
3、向对象添加非正式协议。
2、关于日期(NSDate)的几个常用方法
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
|
NSDateFormatter *FM = [[NSDateFormatter alloc] init];
FM.dateFormat = @
"yyyy-MM-dd HH:mm:ss"
;
NSDate *date1 = [FM dateFromString:@
"2016-12-20 00:00:00"
];
NSDate *date2 = [FM dateFromString:@
"2016-12-21 00:00:00"
];
//date1与当前时间的差(单位:秒)
NSTimeInterval second1 = [date1 timeIntervalSinceNow];
//date1与1970-1-1 08:00:00时间的差(单位:秒)
NSTimeInterval second2 = [date1 timeIntervalSince1970];
//date1与2001-1-1 08:00:00时间的差(单位:秒)
NSTimeInterval second3 = [date1 timeIntervalSinceReferenceDate];
//date1与date2的差(单位:秒)
NSTimeInterval second4 = [date1 timeIntervalSinceDate:date2];
//返回比较早的那个时间
NSDate *earlyDate = [date1 earlierDate:date2];
//返回比较晚的那个时间
NSDate *laterDate = [date1 laterDate:date2];
//判断两个时间是否相等
BOOL isEqual = [date1 isEqualToDate:date2];
//返回当前时间10秒后的时间
NSDate *date01 = [NSDate dateWithTimeIntervalSinceNow:
10
];
//返回1970-1-1 08:00:00时间10秒后的时间
NSDate *date02 = [NSDate dateWithTimeIntervalSince1970:
10
];
//返回2001-1-1 08:00:00时间10秒后的时间
NSDate *date03 = [NSDate dateWithTimeIntervalSinceReferenceDate:
10
];
//返回date2时间10秒后的时间
NSDate *date04 = [NSDate dateWithTimeInterval:
10
sinceDate:date2];
//随机返回一个比较遥远的未来时间
NSDate *date05 = [NSDate distantFuture];
//随机返回一个比较遥远的过去时间
NSDate *date06 = [NSDate distantPast];
|
3、图片拉伸
1
2
3
4
5
6
7
|
//先对图片设置拉伸程度
UIImage *image = [UIImage imageNamed:@
"pic.png"
];
image = [image stretchableImageWithLeftCapWidth:
10
topCapHeight:
5
];
//将图片显示出来
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(
10
,
100
,
100
,
50
)];
imageView.image = image;
[self.view addSubview:imageView];
|
1- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
这个函数是UIImage的一个实例函数,它的功能是创建一个内容可拉伸,而边角不拉伸的图片,需要两个参数,第一个是左边不拉伸区域的宽度,第二个是上面不拉伸的高度。
注意:可拉伸的范围都是距离leftCapWidth后的1竖排像素,和距离topCapHeight后的1横排像素,而图像后面的剩余像素也不会被拉伸。
比如参数指定10,5。那么,图片左边10个像素,上边5个像素。不会被拉伸,x坐标为11和一个像素会被横向复制,y坐标为6的一个像素会被纵向复制。
4、APP跳转/跳转至系统APP
跳转前设置:
1、在将要跳转到的APP中TARGETS--info--URL Types中添加URL Schemes;
2、在本APP的info.plist中添加一个LSApplicationQueriesSchemes数组字段,把对方的APP的URL Schemes添加进去
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//跳转到系统app
url = @
"tel://1234567"
//拨打电话
url = @
"sms://1234567"
//发短信
url = @
"http://www.baidu.com"
//Safari
url = @
"mailto://admin@abt.com"
//邮箱
url = @
"maps://"
//地图
url = @
"facetime://1234567"
//FaceTime
//跳转到系统设置
url = UIApplicationOpenSettingsURLString
//适用于iOS >= 8
url = @
"Prefs:root=General"
//适用于iOS < 10
//跳转第三方APP
url = @
"要跳转的app的URL Schemes"
//开始跳转
if
([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]]) {
if
(IOS_10) {
[[UIApplication sharedApplication] openURL:URL options:@{} completionHandler:nil];
}
else
{
[[UIApplication sharedApplication] openURL:URL];
}
}
|
5、UIView中的坐标转换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
// 将像素point从view中转换到当前视图中,返回在当前视图中的像素值
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
// 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
// 将rect从view中转换到当前视图中,返回在当前视图中的rect
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
例把UITableViewCell中的subview(btn)的frame转换到 controllerA中
// controllerA 中有一个UITableView, UITableView里有多行UITableVieCell,cell上放有一个button
// 在controllerA中实现:
CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];
或
CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];
// 此rc为btn在controllerA中的rect
或当已知btn时:
CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];
或
CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];
|
6、修改Xcode代码自动生成版权信息Copyright ? 2017年 XXX. All rights reserved
如图,就是修改这个
修改方法:打开.xcodeproj工程文件,显示包含内容,会看到一个project.pbxproj文件,打开此文件修改 ORGANIZATIONNAME = "xxx";
7、iOS让button的文字局左或局右对齐
我们首先想到的方法是这个
1
|
button.titleLabel.textAlignment = NSTextAlignmentLeft;
|
突然发现这是无效的,下面正确的设置方式:
1
2
|
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//局左
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//局右
|
8、去除searchBar的灰色背景
近日,在做搜索框UISearchBar的时候,把searchBar放在导航栏的titleView上,当进入下一页然后再返回本页的时候searchBar的灰色背景会闪一下,那么怎么去除这个灰色背景呢?
调用如下方法即可去除灰色背景,哈哈。。。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
- (
void
)removeSearchBarGrayBackColor
{
for
(
int
i =
0
; i < self.searchBar.subviews.count; i++) {
UIView *backView = self.searchBar.subviews[i];
if
([backView isKindOfClass:NSClassFromString(@
"UISearchBarBackground"
)]) {
[backView removeFromSuperview];
[self.searchBar setBackgroundColor:[UIColor clearColor]];
break
;
}
else
{
NSArray * arr = self.searchBar.subviews[i].subviews;
for
(
int
j =
0
; j < arr.count; j++) {
UIView *barView = arr[i];
if
([barView isKindOfClass:NSClassFromString(@
"UISearchBarBackground"
)]) {
[barView removeFromSuperview];
[self.searchBar setBackgroundColor:[UIColor clearColor]];
break
;
}
}
}
}
}
|
9、修改UITabbar顶部分割线颜色
1
2
3
4
5
6
7
8
9
10
11
|
//根据颜色生成一个图片
CGRect rect = CGRectMake(
0
,
0
, SCREEN_WIDTH,
0.3
);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, LineColor.CGColor);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//开始修改UITabbar顶部分割线颜色
[self.tabBar setShadowImage:img];
[self.tabBar setBackgroundImage:[Tools imageWithColor:WHITECOLOR]];
|
10、压缩图片
创建一个UIImage的类
1
2
3
4
|
@
interface
UIImage (Scale)
//压缩图片
- (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage;
@end
|
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
|
@implementation UIImage (Scale)
- (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage
{
CGFloat maxWidth =
640
;
if
(sourceImage.size.width < maxWidth) {
return
sourceImage;
}
CGFloat btWidth =
0
.0f;
CGFloat btHeight =
0
.0f;
if
(sourceImage.size.width > sourceImage.size.height) {
btHeight = maxWidth;
btWidth = sourceImage.size.width * (maxWidth / sourceImage.size.height);
}
else
{
btWidth = maxWidth;
btHeight = sourceImage.size.height * (maxWidth / sourceImage.size.width);
}
CGSize targetSize = CGSizeMake(btWidth, btHeight);
return
[self imageByScalingAndCroppingForSourceImage:sourceImage targetSize:targetSize];
}
- (UIImage *)imageByScalingAndCroppingForSourceImage:(UIImage *)sourceImage targetSize:(CGSize)targetSize
{
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor =
0.0
;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(
0.0
,
0.0
);
if
(CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if
(widthFactor > heightFactor)
scaleFactor = widthFactor;
// scale to fit height
else
scaleFactor = heightFactor;
// scale to fit width
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if
(widthFactor > heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) *
0.5
;
}
else
if
(widthFactor < heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) *
0.5
;
}
}
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if
(newImage == nil) NSLog(@
"could not scale image"
);
UIGraphicsEndImageContext();
return
newImage;
}
@end
|