1.从服务器获取的数据或者其他需要用到的数据,基本上都需要创建一个数据类来进行存储(对MVC有了进一步了解)
2.16进制颜色定义
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rabValue & 0xFF0000) >> 16)) /255.0 green:((float)((rabValue & 0xFF00) >> 8)) /255.0 blue:((float)(rabValue & 0xFF)) /255.0 alpha:1.0];
3.更改导航栏的背景图片
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@“xxx”] forMetrics:UIBarMetricsDefault];
导航栏字体颜色
[[UINavigationBar appearance] setTintColor:[UIColor xxxx];
返回按钮图片
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@""];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@""];
4.如果使用的是UINavigationController,根视图为A,从A进入到B视图中,如果B视图的返回键不需要出现标题的话,在A跳转到B之前(也就是在A的方法里)可以使用
<span style="font-size:18px;">self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStrylePlain target:self action:nil];
来将back的title设置为空
5.自定义的数据类型是无法使用NSUserDefaults来进行存储的,想要达到这个目的时,自定义的数据需要实现initWithCoder和encodeWithCoder两个方法
e.g
Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (nonatomic, strong) NSString *name
@property (nonatomic, strong) NSString *gender;
- (instancetype)initWithCoder:(NSCoder *)coder;
- (void)encodeWithCoder:(NSCoder *)coder;
Student.m
#import "Student.h"
@implementation Student
- (instancetype)init {
self = [super init];
if (self) {
self.name = [[NSString alloc] init];
self.gender = [[NSString alloc] init];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super init];
if (self) {
self.name = [coder decodeObjectForKey:@"_name"];
self.gender = [coder decodeObjectForKey:@"_gender"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:self.name forKey:@"_name"];
[coder encodeObject:self.gender forKey:@"_gender"];
}
要进行保存:
Student *student = [[Student alloc] init];
student.name = @"Zhang San";
student.gender = @"male";
NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:student];
[[NSUserDefaults standardUserDefaults] setObject:archivedData forKey:@"StudentData"];
取出来使用时:
NSData *unarchivedData = [[NSUserDefaults standardUserDefaults] objectForKey:@"StudentData"];
Student *unarchivedStudent = [NSKeyedUnarchiver unarchivedObjectWithData:unarchivedData];
6.当从服务器中获取的数据为null,这时候这份数据没办法直接添加到数组或者字典中,使用isEqualToString进行判断也会崩溃,应该使用isEqual:[NSNull null]进行判断即可
7.判断字符串包含某个内容
NSRange range = [string rangeOfString:@"需要包含的字符串"];
if (range.length> 0) {
// 包含需要的字符串下的操作
}
8.使用UITableView时,需要调整每个section的高度时,使用下面四个函数进行调整
tableView:heightForHeaderInSection
tableView:heightForFooterInSection
tableView:viewForHeaderInSection
tableView:viewForFooterInSection
9.UILabel要根据文字的内容自动调整宽度或者高度
NSString *content = @"xxxxxxxxxxxxxx";
NSDictionary *attribute = @{NSFontAttributeName : [UIFont systemFontOfSize:18.0];//字体类型和大小自己定义
CGSize textSize = [string boundingRectWithSize:CGSizeMake(width, height) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil].size;//第一个参数中得widht和height为自己规定的宽和高的范围, options的话,第一个表示
截断和一个省略号字符添加到最后一个可见行,如果文本不适合指定的范围。第二个表示指定行开头,而不是基准的起点.
10.同一个UITableView使用多个不同类型的Cell时,在拉动时好像会出现卡顿的现象,使用覆用的话会出现cell出现在视线之后,之前的内容才会跳变成应呈现的内容。(这个问题尚不知道如何解决)
11.UIButton作为Cell的subView时,在按下的时候期高亮状态会出现一定的延迟,当时是自己写了一个UIView来解决这个问题。
12.网络图片的设置,还是推荐使用SDWebImage,但是SDWebImage会有一个小bug(也有可能是对SDWebImage不熟悉导致)
使用sd_setImageWithURL:placeholderImage:options:progress:completed:这个方法的时候,如果图片没被成功下载(通过在completed中block,检测参数image.size.width,没被成功下载的时候为0),progress会判定为1,该函数还是会判断为下载完成?
13.动画
CABasicAnimation通常只会用于一个动画,而CAKeyframeAnimation则可以创建多个动画,最后使用CAAnimationGroup将几个动画合并为数组添加到属性animations中,并且设置duration
e.g
CAKeyframeAnimation *animation1 = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
animation1.values = @[@1.0, @1.5, @2.0, @1.0];
animation1.calculationMode = kCAAnimationLinear;
// Animation 2
CAKeyframeAnimation *animation2 = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
animation2.values = @[@(-0.5), @0.5];
animation2.calculationMode = kCAAnimationLinear;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = [NSArray arrayWithObjects:animation1, animation2, nil];
group.duration = 0.5;
[view.iconView.layer addAnimation:group forKey:@"View Animation"];
其中animationWithKeyPath的值有
在动画这个过程中,因为需要刷新TableView的数据,所以遇到不少麻烦。刷新数据的话会刷新整个TableView,动画会被强制停止。后来使用UIView的+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion方法,在最后completion中延时执行reloadData解决了,不过好像有点不合适的样子?
14.使用ShareSDK的问题:ShareSDK中需要新浪微博的话,如果微博没有高级权限的话是没办法使用网络图片的,所以当时遇到的问题是将新浪微博平台和其他平台分开处理!使用addSinaWeiboUnitWithContent方法。但是这样操作的话,用户在分享出去时,在编辑框添加或删除的自定义内容是没办法显示的,因为上面的方法会是最后的分享内容,没办法更改。
15.iOS获取时间戳
long timeInterval = [[NSDate date] timeIntervalSince1970];
NSString *time = [NSString stringWithFormat:@"%ld", timeInterval];
16.产生随机字符串
// 产生n位随机字符串
NSArray *character = @[@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", @"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w", @"x", @"y", @"z"];
NSMutableString *string = [[NSMutableString alloc] init];
for (int i = 0 ; i < n; i++) {
// 产生一个随机数
int n = arc4random() % character.count;
[string appendString:character[n]];
}
NSString *result = [[NSString alloc] initWithString:string];
17.导航栏标题图片
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xxxx"]];
18.导航栏按钮
UIBarButtonItem *item = [[UIBarButtonItem alloc] initIWithImage:[UIImage imageNamed:@"xxx"] style:UIBarButtonItemStyleDone target:self action:@selector()];
[self.navigationItem setRightBarButtonItem:item];
19.GIF图片的处理
GIF使用的是YLImageView类库,而第一帧图片的话则是服务器给的,避免了获取第一帧图片的麻烦。另外的话,gif则是下载到本地之后才播放。下载了以后移除UIImageView,添加YLImageView
下载使用的是AFNetworking的方法
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", filePath);
NSData *data = [NSData dataWithContentsOfFile:filePath];
[MBHud hide:YES];
gifImageView.image = [YLGIFImage imageWithData:data];
[imageView removeFromSuperview];
[self.contentView addSubview:gifImageView];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
MBHud.progress = (float)totalBytesRead / totalBytesExpectedToRead;
if (MBHud.progress < 1) {
imageView.userInteractionEnabled = NO;
}
}];
[operation start];
下载的同时可以通过setDownloadProgressBlock来设置进度
20.如果View是图片的话,想进行Animation更改的话
<span style="font-size:18px;">imageView.layer.contents = (id)[UIImage imageNamed:@"xxx"].CGImage;</span>
21.使用UINavigationController时,如果想要添加一个占满全屏的view时的方法:
UIWindow *window;
UIApplication *app = [UIApplication sharedApplication];
if ([app.delegate respondsToSelector:@selector(window)]) {
window = [app.delegate window];
} else {
window = [app keyWindow];
}
[window addSubview:albumBackground];
22.创建一个简单的图片浏览,镶嵌两个UIScrollView
//-------------------------------------------- 第一层UIScrollView ----------------------------------------------------------//
UIScrollView *bottomView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, MAIN_HEIGHT)];
bottomView.delegate = self;
bottomView.tag = 100;
bottomView.contentSize = CGSizeMake(self.jpgImageURL.count * MAIN_WIDTH, MAIN_HEIGHT);
bottomView.pagingEnabled = YES;
bottomView.showsHorizontalScrollIndicator = YES;
bottomView.backgroundColor = [UIColor clearColor];
[albumBackground addSubview:bottomView];
//-------------------------------------------- 第二层UIScrollView ---------------------------------------------------------//
for (int i = 0; i < self.jpgImageURL.count; i++) {
UIScrollView *secondView = [[UIScrollView alloc] initWithFrame:CGRectMake(MAIN_WIDTH * i, 0, MAIN_WIDTH, MAIN_HEIGHT)];
secondView.contentSize = CGSizeMake(MAIN_WIDTH, MAIN_HEIGHT);
secondView.maximumZoomScale = 3.0;
secondView.minimumZoomScale = 1.0;
secondView.delegate = self;
secondView.tag = 101 + i;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, MAIN_HEIGHT)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.userInteractionEnabled = YES;
[secondView addSubview:imageView];
[bottomView addSubview:secondView];
}
想要令自动跳转的话可以使用[scrollView setContentOffset:CGPointMake(x, y) animated:NO];
获取scrollView当前的索引:在UIScrollViewDelegate的方法中:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
float Width = scrollView.frame.size.width;
int selectedIndex = floor((scrollView.contentOffset.x - Width / 2) / Width) + 2;
self.jpgLabel.text = [NSString stringWithFormat:@"%d / %d", selectedIndex, self.jpgImageURL.count];
}
23.使用UILongPressGestureRecognizer的时候会有一个问题,长按后松开,绑定的动作事件总会调用两次,解决的方法是在需要执行的方法中判定状态
if (sender.state == UIGestureRecognizerStateBegan || sender.state == UIGestureRecognizerStateEnder) {
// 执行操作
}
24.将图片保存到相册
调用UIImageWriteToSavedPhotosAlbum()
void UIImageWriteToSavedPhotosAlbum (
UIImage *image,
id completionTarget,
SEL completionSelector,
void *contextInfo
);
其中completionSelector的话使用- (void) image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo;
25.UITableView滚动到顶点
<span style="font-size:18px;">[tableView setContentOffset:CGPointMake(0,0) animated:YES];</span>
26.需要同时设定UIButton的图片和title的话,可以使用category
- (void)setTitle:(NSString *)title andImage:(UIImage *)image forState:(UIControlState)state
{
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageEdgeInsets = UIEdgeInsetsMake(3.0, 0.0, 10.0, 0.0);
[self setImage:image forState:state];
[self.titleLabel setContentMode:UIViewContentModeLeft];
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
[self.titleLabel setFont:[UIFont systemFontOfSize:13.0]];
[self setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 3.0, 0.0)];
[self setTitle:title forState:state];
}
可是这种方法似乎对于图片大小的限制比较高,当时是调了好久的。但是好处就是省去了自定义Button的麻烦
27.圆形图片,或者头像的话可以使用PAImageView~在Code4app还是github上有
28.SVN的设置
1) Xcode一般都有了SVN,打开终端,输入svn -version
一般会出现
2)然后编辑文件 open ~/.subversion/config 打开文件
找到global-ignores一行,去掉注释,改成global-ignores = build *~.nib *.so *.pbxuser *.mode *.perspective*
3)找到 enable-auto-props = yes 把注释去掉,在[auto-props] Section声明以下文本文件
*.pbxuser = svn:mime-type=text/X-xcode
*.perspective* = svn:mime-type=text/X-xcode
*.pbxproj = svn:mime-type=text/X-xcode
(上面这几步是在网上找的步骤)
4)Xcode -> Preferences
添加一个Repository,
输入地址,用户名密码等
6)成功后点击
Source Control -> Check out,把目录下载到你想放的位置
7)将你的项目代码copy到刚刚下载的文件夹中,重新打开之后在Source Control就可以commit和update的操作了。(这里建议将代码copy,然后将副本拉到文件夹中。。第一次操作的时候就是因为不会操作然后改过bug的版本代码都不见了。。。)