記錄下一些不常用的技巧,以防忘記,複製用。 1、獲取當前的View在Window的frame: 2、UIImageView 和UILabel 等一些控制項,需要加這句才能setCorn 3、手機上的沙盒路徑要加"Documents",不然存儲寫入失敗!mac上不用! 4、圖片拉伸不失真,如聊天軟體對話 ...
記錄下一些不常用的技巧,以防忘記,複製用。
1、獲取當前的View在Window的frame:
UIWindow * window=[[[UIApplication sharedApplication] delegate] window]; CGRect rect=[_myButton convertRect:_myButton.bounds toView:window];
2、UIImageView 和UILabel 等一些控制項,需要加這句才能setCorn
_myLabel.layer.masksToBounds = YES;
3、手機上的沙盒路徑要加"Documents",不然存儲寫入失敗!mac上不用!
[_myArray writeToFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"shopCategory.plist"] atomically:YES]; NSArray *tempAllData = [NSArray arrayWithContentsOfFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"shopCategory.plist"]];
4、圖片拉伸不失真,如聊天軟體對話氣泡
1)、方法1,比較老的,
UIImage *tempImage2 = [UIImage imageNamed:@"sub.png"]; tempImage2 = [tempImage2 stretchableImageWithLeftCapWidth:tempImage2.size.width/2 topCapHeight:0];
2)、方法2,比較新的
UIImage *tempImage3 = [UIImage imageNamed:@"sub"]; CGFloat tempH = tempImage3.size.height/2; CGFloat tempW = tempImage3.size.width/2; UIEdgeInsets tempEdg = UIEdgeInsetsMake(tempH, tempW, tempH, tempW); tempImage3 = [tempImage3 resizableImageWithCapInsets:tempEdg resizingMode:UIImageResizingModeStretch];
5、視頻截取縮略圖,其中CMTimeMakeWithSeconds(5,1),調整截圖幀數/秒數,一般不用特意去修改,不做參數傳入,除非片頭一段時間都一樣的視頻。
#import <AVFoundation/AVFoundation.h> -(UIImage *)getThumbnailImage:(NSString *)videoURL { AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoURL] options:nil]; AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset]; gen.appliesPreferredTrackTransform = YES; //控制截取時間 CMTime time = CMTimeMakeWithSeconds(5, 1); NSError *error = nil; CMTime actualTime; CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error]; UIImage *thumb = [[UIImage alloc] initWithCGImage:image]; CGImageRelease(image); return thumb; }
6、cell下劃線左邊頂住屏幕左邊。
cell.preservesSuperviewLayoutMargins = NO; cell.layoutMargins = UIEdgeInsetsZero; cell.separatorInset = UIEdgeInsetsZero;
7、去除xcode8冗餘信息,雖然已經記住了。
OS_ACTIVITY_MODE disable
8、播放音頻
1)工程內音頻
1-1)、獲取音頻路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"shakebell" ofType:@"wav"]; NSURL *url = [NSURL fileURLWithPath:path];
1-2)、創建音頻播放ID
SystemSoundID soundID; AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);
1-3)、Play
AudioServicesPlaySystemSound(soundID);
2)系統音頻,參數為1000-1351,具體查表,如1007為“sms-received1”
AudioServicesPlaySystemSound(1007);
9、字體自適應
1)、固定的Frame,自適應Font大小,如數量增減,1和1000。
[label1 setAdjustsFontSizeToFitWidth:YES];
2)、固定的Font,自適應Frame,用於信息類顯示
[label2 sizeToFit];
3)、固定的Font,獲取自適應Frame值,反過來設置Label的Frame,用於信息類顯示。這裡的100是等下設置Label的width,也是返回的rect.frame.size.width
CGRect rect = [templabel.text boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:templabel.font} context:nil];
10、AFNetworking 檢測網路連接狀態
[[AFNetworkReachabilityManager sharedManager]startMonitoring]; [[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { NSLog(@"%ld",status); }];
11、編輯相關
1)鍵盤事件通知
1-1)、彈出鍵盤可能蓋住TextField。監聽鍵盤的通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moveView:) name:UIKeyboardDidChangeFrameNotification object:nil];
1-2)、moveView方法里接收通知,tempTime是鍵盤動畫時間,tempY是鍵盤當前的y軸位置。(接著要移動評論框或者移動後面的ScrollView都可以)
CGFloat tempTime = [[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; CGFloat tempY = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].origin.y //重置約束條件 //self.theBottomSpace.constant = ?; [UIView animateWithDuration:duration animations:^{ //更新約束 [self.view layoutIfNeeded]; }];
2)dealloc記得移除
[[NSNotificationCenter defaultCenter]removeObserver:self];
3)touchesBegan:withEvent && scrollViewDidScroll -->屏幕點擊&&屏幕滑動要取消編輯狀態
[self.view endEditing:YES];
12、上傳圖片(頭像)
1-1)、把Image打成NSData
NSData *imagedata = UIImageJPEGRepresentation(tempImage, 1.0);
1-2)、AFNetworking的POST方法填如下。formData:POST方法里的Block參數,name:跟伺服器有關,filename:隨意填,mimeType:就image/jpg。
[formData appendPartWithFileData:imagedata name:@"imgFile" fileName:@"idontcare.jpg" mimeType:@"image/jpg"];
13、強制佈局
[self.view layoutIfNeeded];
14、圖片雙擊縮放
1)scrollView才可以縮放,所以要把ImageView加在scrollView,給scrollView(這裡的 self )添加手勢識別。要設最大/小縮放比例!
UITapGestureRecognizer *imageTwoTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(twoTapAction:)]; imageTwoTap.numberOfTapsRequired = 2; [self addGestureRecognizer: imageTwoTap];
2)第一次點哪放大哪,第二次恢複原來大小
#define SCALE_WIDTH 60 //要放大的局部寬度大小 #define SCALE_HEIGHT 60 //要放大的局部高度大小 -(void)twoTapAction:(UITapGestureRecognizer *)tempTap { if (self.zoomScale != 1.0) { [self setZoomScale:1.0 animated:YES]; } else { CGPoint tempPoint = [tempTap locationInView:self]; [self zoomToRect:CGRectMake(tempPoint.x-SCALE_WIDTH/2, tempPoint.y-SCALE_HEIGHT/2, SCALE_WIDTH, SCALE_HEIGHT) animated:YES]; } }