iOS开发 小知识点
退回輸入鍵盤:
- (BOOL) textFieldShouldReturn:(id)textField{[textField resignFirstResponder]; }CGRect
CGRect frame = CGRectMake (origin.x, origin.y, size.width, size.height);矩形 NSStringFromCGRect(someCG) 把CGRect結(jié)構(gòu)轉(zhuǎn)變?yōu)楦袷交址?#xff1b; CGRectFromString(aString) 由字符串恢復(fù)出矩形; CGRectInset(aRect) 創(chuàng)建較小或較大的矩形(中心點(diǎn)相同),+較小 -較大 CGRectIntersectsRect(rect1, rect2) 判斷兩矩形是否交叉,是否重疊 CGRectZero 高度和寬度為零的/位于(0,0)的矩形常量CGPoint & CGSize
CGPoint aPoint = CGPointMake(x, y); CGSize aSize = CGSizeMake(width, height);設(shè)置透明度
[myView setAlpha:value]; (0.0 < value < 1.0)設(shè)置背景色
[myView setBackgroundColor:[UIColor redColor]];(blackColor;darkGrayColor;lightGrayColor;whiteColor;grayColor; redColor; greenColor; blueColor; cyanColor;yellowColor;magentaColor; orangeColor;purpleColor;brownColor; clearColor; )自定義顏色:
UIColor *newColor = [[UIColor alloc] initWithRed:(float) green:(float) blue:(float) alpha:(float)]; 0.0~1.0寬度和高度
768X1024 1024X768 狀態(tài)欄高 20 像素高 導(dǎo)航欄 工具欄 44像素高隱藏狀態(tài)欄:
[[UIApplication shareApplication] setStatusBarHidden: YES animated:NO]橫屏:
[[UIApplication shareApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]. orientation == UIInterfaceOrientationLandscapeLeft window=[[UIWindow alloc] initWithFrame:[UIScreen mainScreen] bounds];全屏自動(dòng)適應(yīng)父視圖大小:
aView.autoresizingSubviews = YES; aView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);定義按鈕
UIButton *scaleUpButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [scaleUpButton setTitle:@"放 大" forState:UIControlStateNormal]; scaleUpButton.frame = CGRectMake(40, 420, 100, 40); [scaleUpButton addTarget:self action:@selector(scaleUp) forControlEvents:UIControlEventTouchUpInside];設(shè)置視圖背景圖片
UIImageView *aView; [aView setImage:[UIImage imageNamed:@”name.png”]]; view1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image1.png"]];UISlider *slider = (UISlider *) sender; NSString *newText = [[NSString alloc] initWithFormat:@”%d”, (int)(slider.value + 0.5f)]; label.text = newText;活動(dòng)表單 <UIActionSheetDelegate>
- (IBActive) someButtonPressed:(id) sender {UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@”Are you sure?”delegate:selfcancelButtonTitle:@”No way!”destructiveButtonTitle:@”Yes, I’m Sure!”otherButtonTitles:nil];[actionSheet showInView:self.view];[actionSheet release]; }警告視圖 <UIAlertViewDelegate>
- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {if(buttonIndex != [actionSheet cancelButtonIndex]){NSString *message = [[NSString alloc] initWithFormat:@”You canbreathe easy, everything went OK.”];UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@”Something was done”message:messagedelegate:selfcancelButtonTitle:@”O(jiān)K”otherButtonTitles:nil];[alert show];[alert release];[message release];} }動(dòng)畫效果
-(void)doChange:(id)sender { if(view2 == nil) { [self loadSec]; } [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1]; [UIView setAnimationTransition:([view1 superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)forView : self.view cache:YES];if([view1 superview]!= nil) { [view1 removeFromSuperview]; [self.view addSubview:view2];}else {[view2 removeFromSuperview]; [self.view addSubview:view1]; } [UIView commitAnimations]; }Table View <UITableViewDateSource>
#pragma mark - #pragma mark Table View Data Source Methods //指定分區(qū)中的行數(shù),默認(rèn)為1 - (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section { return [self.listData count]; }//設(shè)置每一行cell顯示的內(nèi)容 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *SimpleTableIndentifier = @"SimpleTableIndentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIndentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIndentifier] autorelease]; }UIImage *image = [UIImage imageNamed:@"13.gif"]; cell.imageView.image = image;NSUInteger row = [indexPath row]; cell.textLabel.text = [listData objectAtIndex:row];cell.textLabel.font = [UIFont boldSystemFontOfSize:20];if(row < 5) cell.detailTextLabel.text = @"Best friends"; elsecell.detailTextLabel.text = @"friends"; return cell; }圖像:如果設(shè)置圖像,則它顯示在文本的左側(cè)
文本標(biāo)簽:這是單元的主要文本(UITableViewCellStyleDefault 只顯示文本標(biāo)簽)
詳細(xì)文本標(biāo)簽:這是單元的輔助文本,通常用作解釋性說明或標(biāo)簽
UITableViewCellStyleSubtitle UITableViewCellStyleDefault UITableViewCellStyleValue1 UITableViewCellStyleValue2<UITableViewDelegate> #pragma mark - #pragma mark Table View Delegate Methods //把每一行縮進(jìn)級(jí)別設(shè)置為其行號(hào) - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; return row; } //獲取傳遞過來的indexPath值 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; if (row == 0) return nil; return indexPath; }- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; NSString *rowValue = [listData objectAtIndex:row]; NSString *message = [[NSString alloc] initWithFormat:@"You selected %@",rowValue]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selected" message:messagedelegate:nilcancelButtonTitle:@"Yes, I did!"otherButtonTitles:nil]; [alert show]; [alert release]; [message release]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; }//設(shè)置行的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 40; }隨機(jī)數(shù)的使用
頭文件的引用#import <time.h>#import <mach/mach_time.h>srandom()的使用srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF));直接使用 random() 來調(diào)用隨機(jī)數(shù)在UIImageView 中旋轉(zhuǎn)圖像
float rotateAngle = M_PI;CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);imageView.transform = transform;以上代碼旋轉(zhuǎn)imageView, 角度為rotateAngle, 方向可以自己測(cè)試哦!
在Quartz中如何設(shè)置旋轉(zhuǎn)點(diǎn)
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];imageView.layer.anchorPoint = CGPointMake(0.5, 1.0);這個(gè)是把旋轉(zhuǎn)點(diǎn)設(shè)置為底部中間。記住是在QuartzCore.framework中才得到支持。
創(chuàng)建.plist文件并存儲(chǔ)
NSString *errorDesc; //用來存放錯(cuò)誤信息NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:4]; //NSDictionary, NSData等文件可以直接轉(zhuǎn)化為plist文件NSDictionary *innerDict;NSString *name;Player *player;NSInteger saveIndex;for(int i = 0; i < [playerArray count]; i++) {player = nil;player = [playerArray objectAtIndex:i];if(player == nil)break;name = player.playerName;// This “Player1″ denotes the player name could also be the computer nameinnerDict = [self getAllNodeInfoToDictionary:player];[rootObj setObject:innerDict forKey:name]; // This “Player1″ denotes the person who start this game}player = nil;NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];最后2行可以忽略,只是給rootObj添加一點(diǎn)內(nèi)容。這個(gè)plistData為創(chuàng)建好的plist文件,用其writeToFile方法就可以寫成文件。下面是代碼:
/*得到移動(dòng)設(shè)備上的文件存放位置*/NSString *documentsPath = [self getDocumentsDirectory];NSString *savePath = [documentsPath stringByAppendingPathComponent:@"save.plist"];/*存文件*/if (plistData) {[plistData writeToFile:savePath atomically:YES];}else {NSLog(errorDesc);[errorDesc release];}- (NSString *)getDocumentsDirectory {NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);return [paths objectAtIndex:0];}讀取plist文件并轉(zhuǎn)化為NSDictionary
NSString *documentsPath = [self getDocumentsDirectory];NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"save.plist"];NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];讀取一般性文檔文件
NSString *tmp;NSArray *lines; /*將文件轉(zhuǎn)化為一行一行的*/lines = [[NSString stringWithContentsOfFile:@"testFileReadLines.txt"]componentsSeparatedByString:@”\n”];NSEnumerator *nse = [lines objectEnumerator];// 讀取<>里的內(nèi)容while(tmp = [nse nextObject]) {NSString *stringBetweenBrackets = nil;NSScanner *scanner = [NSScanner scannerWithString:tmp];[scanner scanUpToString:@"<" intoString:nil];[scanner scanString:@"<" intoString:nil];[scanner scanUpToString:@">" intoString:&stringBetweenBrackets];NSLog([stringBetweenBrackets description]);}對(duì)于讀寫文件,還有補(bǔ)充,暫時(shí)到此。隨機(jī)數(shù)和文件讀寫在游戲開發(fā)中經(jīng)常用到。所以把部分內(nèi)容放在這,以便和大家分享,也當(dāng)記錄,便于查找。
隱藏NavigationBar
[self.navigationController setNavigationBarHidden:YES animated:YES];在想隱藏的ViewController中使用就可以了。
如果無法保證子類行為的一致性,那么就用委托
If the subClass cann’t keep with superClass,use delegate rather than inheritance.
屏幕上看到的,都是UIVew
Everything you see on Screen is UIView.
如果對(duì)性能要求高,慎用Interface Build
if application’s performance is important,be discreet for the interface build.
copy是創(chuàng)建,retain是引用
the copy operation is create a new one,but the retain operation is just a reference.
alloc需要release,convenient不需要release
alloc method need corresponding release method,but convenient method not.
加載到NSArray/NSMutableArray里的對(duì)象,不需要負(fù)責(zé)release
The objects added to NSArray/NSMutableArray need not to be released.
IBOutlet,IBAction為你開啟了訪問Interface Build中對(duì)象的大門
IBOutlet and IBAction open the door to access the objects in Interface build.
UIApplicationDelegate負(fù)責(zé)應(yīng)用程序的生命周期,而UIViewController負(fù)責(zé)View的生命周期
UIApplicationDelegate is responsible for the application life cycle,but UIViewController for the UIView.
為了程序的健壯性,請(qǐng)盡量實(shí)現(xiàn)Delegate的生命周期函數(shù)
if you want to develop a robust application,implement the life cycle methods as more as possbile.
you觸摸的不是UIEvent,而是NSSet的UIView
what you touch on screen is not UIEvent but UIView
UITextField不響應(yīng)鍵盤:
方法1: TextField的的Touch Cancel響應(yīng)中,添加[textFied resignFirstResponder];方法: - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{[textFied resignFirstResponder]; }更改響應(yīng)鍵盤return按鈕:
TextField.returnKeyType=UIReturnKeyDone; select:UIReturnKeyDefault,UIReturnKeyGo,UIReturnKeyGoogle,UIReturnKeyJoin,UIReturnKeyNext,UIReturnKeyRoute,UIReturnKeySearch,UIReturnKeySend,UIReturnKeyYahoo,UIReturnKeyDone,UIReturnKeyEmergencyCall,尺寸問題:
iPhone應(yīng)用程序圖標(biāo)大小:57*57;iPhone全屏UIView大小:320*460 添加UITabBar后大小:320*411UITabelViewCell默認(rèn)大小: 320*44繪制控件方法
//--alloc -(UITextField *)GetDefaultTextField:(CGRect)frame{UITextField *textField=[[UITextField alloc] initWithFrame:frame];textField.borderStyle=UITextBorderStyleRoundedRect;textField.font=[UIFont fontWithName:@"Arial" size:12.0];textField.textAlignment=UITextAlignmentCenter;textField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;textField.keyboardType=UIKeyboardTypeNumbersAndPunctuation;textField.returnKeyType=UIReturnKeyDone;textField.delegate=self;return textField;} //--alloc -(UILabel *)GetDefaultLabel:(CGRect)frame{UILabel *label = [[UILabel alloc] initWithFrame: frame];label.textAlignment=UITextAlignmentCenter;label.textColor=[UIColor blackColor];label.backgroundColor=[UIColor clearColor];label.font=[UIFont boldSystemFontOfSize:12.0];return label; } //--alloc -(UIButton *)GetDefaultButton:(CGRect)frame{UIButton *button=[[UIButton alloc] initWithFrame:frame];[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];[button.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0]];[button.titleLabel setLineBreakMode:UILineBreakModeCharacterWrap];[button addTarget:self action:@selector(btnTradeTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];[button setBackgroundImage:[UIImage imageNamed:@"png1.png"] forState:UIControlStateNormal];[button setBackgroundColor:[UIColor lightGrayColor]];button.tag=kButtonTag;return button;}多使用宏定義常量。tag,frame大小,一些判斷標(biāo)志位。
#define kIndexValueTag 1蘋果屏幕截圖快捷鍵
一般在Mac上用Command-Shif-3/4來截圖。注:Command=蘋果鍵 其實(shí)還有幾個(gè)輔助鍵,來起到不同的截圖功能……
1)Command-Shift-3(適用于OS9,10.1X和10.2) 將整個(gè)屏幕拍下并保存到桌面。 2)Command-Shift-4(適用于OS9,10.1X和10.2) 將屏幕的一部分拍下并保存到桌面。當(dāng)按下著幾個(gè)鍵后,光標(biāo)會(huì)變?yōu)橐粋€(gè)十字,可以拖拉來選取拍報(bào)區(qū)域。 3)Command-Shift-Control-3(適用于OS9和10.2) 將整個(gè)屏幕拍下并保存到剪貼板,可以Command+V直接粘貼到如Photoshop等軟件中編輯。 4)Command-Shift-Control-4(適用于OS9和10.2) 將屏幕的一部分拍下并保存到剪貼板。 5)Command-Shift-4再按空格鍵(適用于10.2) 光標(biāo)會(huì)變成一個(gè)照相機(jī),點(diǎn)擊可拍下當(dāng)前窗口或菜單或Dock以及圖標(biāo)等,只要將照相機(jī)移動(dòng)到不用區(qū)域(有效區(qū)域會(huì)顯示為淺藍(lán)色)點(diǎn)擊。 6)Command-Shift-Control-4再按空格鍵(適用于10.2) 將選取的窗口或其他區(qū)域的快照保存到剪貼板。 7)Command-Shift-Capslock-4(適用于OS9) 將當(dāng)前的窗口拍下并保存到桌面。 8)Command-Shift-Capslock-Control-4(適用于OS9) 將當(dāng)前的窗口拍下并保存到剪貼板。 posted @ 2011-08-21 19:58 ccfzy 閱讀(510) 評(píng)論(0)?編輯 iphone用法大合集 摘要: 轉(zhuǎn):http://blog.163.com/yonglike@yeah/blog/static/168299425201141743714116/1. stringWithFormat 用法: [NSString stringWithFormat:@"Hight: %d°%@ Low: %d°%@", [Temp],@"C",[lTemp],@"C"]; NSString to NSData:NSString* str= @"kilonet";NSData* data=[str dataUsin閱讀全文 posted @ 2011-08-21 17:59 ccfzy 閱讀(993) 評(píng)論(0)?編輯 NavigationController 上面添加按鈕兩種方法?
?
1:
self.navigationItem.leftBarButtonItem?= [[[UIBarButtonItem?alloc]?
? initWithBarButtonSystemItem:UIBarButtonSystemItemAction
? target:selfaction:@selector(ids:)] autorelease];
?
2:
UIBarButtonItem?*backButton = [[UIBarButtonItem?alloc]?initWithTitle:@"反饋"?style:UIBarButtonItemStylePlain?target:nil?action:nil];
self.navigationItem.leftBarButtonItem?= backButton;
?
[backButton?release];
?
posted @ 2011-08-21 15:23 ccfzy 閱讀(200) 評(píng)論(0)?編輯 2011年6月27日 copy, tetain, assign , readonly , readwrite, nonatomic區(qū)別copy是創(chuàng)建一個(gè)新對(duì)象,retain是創(chuàng)建一個(gè)指針,引用對(duì)象計(jì)數(shù)加1。
1.readonly表示這個(gè)屬性是只讀的,就是只生成getter方法,不會(huì)生成setter方法.
2.readwrite,設(shè)置可供訪問級(jí)別
3.retain,是說明該屬性在賦值的時(shí)候,先release之前的值,然后再賦新值給屬性,引用再加1。
4.nonatomic,非原子性訪問,不加同步,多線程并發(fā)訪問會(huì)提高性能。先釋放原先變量,再將新變量retaii然后賦值;
? ? ??注意,如果不加此屬性,則默認(rèn)是兩個(gè)訪問方法都為原子型事務(wù)訪問。
posted @ 2011-06-27 23:58 ccfzy 閱讀(89) 評(píng)論(0)?編輯 @property and @synthesize區(qū)別java的基礎(chǔ)理解的。
@property 相當(dāng)于在java中的接口中使用,替代get ?set方法,沒有具體的方法操作。
@synthesize 相當(dāng)于在java中的類中使用,替代get ?set方法,替代的是有方法的操作部分。
posted @ 2011-06-27 21:25 ccfzy 閱讀(32) 評(píng)論(0)?編輯轉(zhuǎn)載于:https://www.cnblogs.com/DamonTang/archive/2012/07/20/2601324.html
總結(jié)
以上是生活随笔為你收集整理的iOS开发 小知识点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MFC(六)(对话框)
- 下一篇: spring AoP学习 -----Ao