iPad开发相关的技巧、知识等等的笔记,比较乱,姑且称作乱乱记吧:P
退回输入键盘:
1 | - (BOOL) textFieldShouldReturn:(id)textField{ |
2 | [textField resignFirstResponder]; |
CGRect
1 | CGRect frame = CGRectMake (origin.x, origin.y, size.width, size.height);矩形 |
2 | NSStringFromCGRect(someCG) 把CGRect结构转变为格式化字符串; |
3 | CGRectFromString(aString) 由字符串恢复出矩形; |
4 | CGRectInset(aRect) 创建较小或较大的矩形(中心点相同),+较小 -较大 |
5 | CGRectIntersectsRect(rect1, rect2) 判断两矩形是否交叉,是否重叠 |
6 | CGRectZero 高度和宽度为零的/位于(0,0)的矩形常量 |
CGPoint & CGSize
1 | CGPoint aPoint = CGPointMake(x, y); CGSize aSize = CGSizeMake(width, height); |
设置透明度
1 | [myView setAlpha:value]; (0.0 < value < 1.0) |
设置背景色
1 | [myView setBackgroundColor:[UIColor redColor]]; |
2 | (blackColor;darkGrayColor;lightGrayColor;whiteColor;grayColor; redColor; greenColor; blueColor; cyanColor;yellowColor;magentaColor; |
3 | orangeColor;purpleColor;brownColor; clearColor; ) |
自定义颜色:
1 | UIColor *newColor = [[UIColor alloc] initWithRed:(float) green:(float) blue:(float) alpha:(float)]; 0.0~1.0 |
宽度和高度
1 | 768X1024 1024X768 状态栏高 20 像素高 导航栏 工具栏 44像素高 |
隐藏状态栏:
1 | [[UIApplication shareApplication] setStatusBarHidden: YES animated:NO] |
横屏:
1 | [[UIApplication shareApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]. |
2 | orientation == UIInterfaceOrientationLandscapeLeft |
3 | window=[[UIWindow alloc] initWithFrame:[UIScreen mainScreen] bounds];全屏 |
自动适应父视图大小:
1 | aView.autoresizingSubviews = YES; |
2 | aView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); |
定义按钮
1 | UIButton *scaleUpButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
2 | [scaleUpButton setTitle:@ "放 大" forState:UIControlStateNormal]; |
3 | scaleUpButton.frame = CGRectMake(40, 420, 100, 40); |
4 | [scaleUpButton addTarget:self action:@selector(scaleUp) forControlEvents:UIControlEventTouchUpInside]; |
设置视图背景图片
2 | [aView setImage:[UIImage imageNamed:@”name.png”]]; |
3 | view1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@ "image1.png" ]]; |
5 | UISlider *slider = (UISlider *) sender; |
6 | NSString *newText = [[NSString alloc] initWithFormat:@”%d”, (int)(slider.value + 0.5f)]; |
活动表单 <UIActionSheetDelegate>
01 | - (IBActive) someButtonPressed:(id) sender |
03 | UIActionSheet *actionSheet = [[UIActionSheet alloc] |
04 | initWithTitle:@”Are you sure?” |
06 | cancelButtonTitle:@”No way!” |
07 | destructiveButtonTitle:@”Yes, I’m Sure!” |
08 | otherButtonTitles:nil]; |
09 | [actionSheet showInView:self.view]; |
10 | [actionSheet release]; |
警告视图 <UIAlertViewDelegate>
01 | - (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex |
03 | if (buttonIndex != [actionSheet cancelButtonIndex]) |
05 | NSString *message = [[NSString alloc] initWithFormat:@”You can |
06 | breathe easy, everything went OK.”]; |
07 | UIAlertView *alert = [[UIAlertView alloc] |
08 | initWithTitle:@”Something was done” |
11 | cancelButtonTitle:@”OK” |
12 | otherButtonTitles:nil]; |
动画效果
01 | -(void)doChange:(id)sender |
07 | [UIView beginAnimations:nil context:NULL]; |
08 | [UIView setAnimationDuration:1]; |
09 | [UIView setAnimationTransition:([view1 superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)forView : self.view cache:YES]; |
11 | if ([view1 superview]!= nil) |
13 | [view1 removeFromSuperview]; |
14 | [self.view addSubview:view2]; |
18 | [view2 removeFromSuperview]; |
19 | [self.view addSubview:view1]; |
21 | [UIView commitAnimations]; |
Table View <UITableViewDateSource>
02 | #pragma mark Table View Data Source Methods |
04 | - (NSInteger)tableView:(UITableView *)tableView |
05 | numberOfRowsInSection:(NSInteger)section |
07 | return [self.listData count ]; |
11 | - (UITableViewCell *)tableView:(UITableView *)tableView |
12 | cellForRowAtIndexPath:(NSIndexPath *)indexPath |
14 | static NSString *SimpleTableIndentifier = @ "SimpleTableIndentifier" ; |
15 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIndentifier]; |
17 | cell = [[[UITableViewCell alloc] |
18 | initWithStyle:UITableViewCellStyleSubtitle |
19 | reuseIdentifier:SimpleTableIndentifier] |
22 | UIImage *image = [UIImage imageNamed:@ "13.gif" ]; |
23 | cell.imageView.image = image; |
25 | NSUInteger row = [indexPath row]; |
26 | cell.textLabel.text = [listData objectAtIndex:row]; |
27 | cell.textLabel.font = [UIFont boldSystemFontOfSize:20]; |
30 | cell.detailTextLabel.text = @ "Best friends" ; |
32 | cell.detailTextLabel.text = @ "friends" ; |
图像:如果设置图像,则它显示在文本的左侧
文本标签:这是单元的主要文本(UITableViewCellStyleDefault 只显示文本标签)
详细文本标签:这是单元的辅助文本,通常用作解释性说明或标签
01 | UITableViewCellStyleSubtitle |
02 | UITableViewCellStyleDefault |
03 | UITableViewCellStyleValue1 |
04 | UITableViewCellStyleValue2 |
08 | #pragma mark Table View Delegate Methods |
10 | - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath |
12 | NSUInteger row = [indexPath row]; |
16 | - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath |
18 | NSUInteger row = [indexPath row]; |
24 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
26 | NSUInteger row = [indexPath row]; |
27 | NSString *rowValue = [listData objectAtIndex:row]; |
28 | NSString *message = [[NSString alloc] initWithFormat:@ "You selected %@" ,rowValue]; |
29 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@ "Row Selected" |
32 | cancelButtonTitle:@ "Yes, I did!" |
33 | otherButtonTitles:nil]; |
37 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
41 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath |
随机数的使用
3 | #import <mach/mach_time.h> |
6 | srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF)); |
在UIImageView 中旋转图像
1 | float rotateAngle = M_PI; |
2 | CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle); |
3 | imageView.transform = transform; |
以上代码旋转imageView, 角度为rotateAngle, 方向可以自己测试哦!
在Quartz中如何设置旋转点
1 | UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@ "bg.png" ]]; |
2 | imageView.layer.anchorPoint = CGPointMake(0.5, 1.0); |
这个是把旋转点设置为底部中间。记住是在QuartzCore.framework中才得到支持。
创建.plist文件并存储
02 | NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:4]; |
03 | NSDictionary *innerDict; |
08 | for (int i = 0; i < [playerArray count ]; i++) { |
10 | player = [playerArray objectAtIndex:i]; |
13 | name = player.playerName; |
14 | innerDict = [self getAllNodeInfoToDictionary:player]; |
15 | [rootObj setObject:innerDict forKey:name]; |
18 | NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc]; |
最后2行可以忽略,只是给rootObj添加一点内容。这个plistData为创建好的plist文件,用其writeToFile方法就可以写成文件。下面是代码:
02 | NSString *documentsPath = [self getDocumentsDirectory]; |
03 | NSString *savePath = [documentsPath stringByAppendingPathComponent:@ "save.plist" ]; |
07 | [plistData writeToFile:savePath atomically:YES]; |
14 | - (NSString *)getDocumentsDirectory { |
15 | NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
16 | return [paths objectAtIndex:0]; |
读取plist文件并转化为NSDictionary
1 | NSString *documentsPath = [self getDocumentsDirectory]; |
2 | NSString *fullPath = [documentsPath stringByAppendingPathComponent:@ "save.plist" ]; |
3 | NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath]; |
读取一般性文档文件
03 | lines = [[NSString stringWithContentsOfFile:@ "testFileReadLines.txt" ] |
04 | componentsSeparatedByString:@”/n”]; |
06 | NSEnumerator *nse = [lines objectEnumerator]; |
09 | while (tmp = [nse nextObject]) { |
10 | NSString *stringBetweenBrackets = nil; |
11 | NSScanner *scanner = [NSScanner scannerWithString:tmp]; |
12 | [scanner scanUpToString:@ "<" intoString:nil]; |
13 | [scanner scanString:@ "<" intoString:nil]; |
14 | [scanner scanUpToString:@ ">" intoString:&stringBetweenBrackets]; |
16 | NSLog([stringBetweenBrackets description]); |
对于读写文件,还有补充,暂时到此。随机数和文件读写在游戏开发中经常用到。所以把部分内容放在这,以便和大家分享,也当记录,便于查找。
隐藏NavigationBar
1 | [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.
如果对性能要求高,慎用Interface Build
if application’s performance is important,be discreet for the interface build.
copy是创建,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里的对象,不需要负责release
The objects added to NSArray/NSMutableArray need not to be released.
IBOutlet,IBAction为你开启了访问Interface Build中对象的大门
IBOutlet and IBAction open the door to access the objects in Interface build.
UIApplicationDelegate负责应用程序的生命周期,而UIViewController负责View的生命周期
UIApplicationDelegate is responsible for the application life cycle,but UIViewController for the UIView.
为了程序的健壮性,请尽量实现Delegate的生命周期函数
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不响应键盘:
1 | 方法1: TextField的的Touch Cancel响应中,添加[textFied resignFirstResponder]; |
3 | 方法: - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ |
5 | [textFied resignFirstResponder]; } |
更改响应键盘return按钮:
01 | TextField.returnKeyType=UIReturnKeyDone; |
13 | UIReturnKeyEmergencyCall, |
尺寸问题:
3 | iPhone全屏UIView大小:320*460 添加UITabBar后大小:320*411 |
5 | UITabelViewCell默认大小: 320*44 |
绘制控件方法
02 | -(UITextField *)GetDefaultTextField:(CGRect)frame{ |
04 | UITextField *textField=[[UITextField alloc] initWithFrame:frame]; |
05 | textField.borderStyle=UITextBorderStyleRoundedRect; |
06 | textField.font=[UIFont fontWithName:@ "Arial" size:12.0]; |
07 | textField.textAlignment=UITextAlignmentCenter; |
08 | textField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter; |
09 | textField.keyboardType=UIKeyboardTypeNumbersAndPunctuation; |
10 | textField.returnKeyType=UIReturnKeyDone; |
11 | textField.delegate=self; |
16 | -(UILabel *)GetDefaultLabel:(CGRect)frame{ |
18 | UILabel *label = [[UILabel alloc] initWithFrame: frame]; |
19 | label.textAlignment=UITextAlignmentCenter; |
20 | label.textColor=[UIColor blackColor]; |
21 | label.backgroundColor=[UIColor clearColor]; |
22 | label.font=[UIFont boldSystemFontOfSize:12.0]; |
26 | -(UIButton *)GetDefaultButton:(CGRect)frame{ |
28 | UIButton *button=[[UIButton alloc] initWithFrame:frame]; |
29 | [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; |
30 | [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]; |
31 | [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; |
32 | [button.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0]]; |
33 | [button.titleLabel setLineBreakMode:UILineBreakModeCharacterWrap]; |
34 | [button addTarget:self action:@selector(btnTradeTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; |
35 | [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter]; |
37 | [button setBackgroundImage:[UIImage imageNamed:@ "png1.png" ] forState:UIControlStateNormal]; |
38 | [button setBackgroundColor:[UIColor lightGrayColor]]; |
39 | button.tag=kButtonTag; |
多使用宏定义常量。tag,frame大小,一些判断标志位。
1 | #define kIndexValueTag 1 |
苹果屏幕截图快捷键
一般在Mac上用Command-Shif-3/4来截图。注:Command=苹果键 其实还有几个辅助键,来起到不同的截图功能……
01 | 1)Command-Shift-3(适用于OS9,10.1X和10.2) |
03 | 2)Command-Shift-4(适用于OS9,10.1X和10.2) |
04 | 将屏幕的一部分拍下并保存到桌面。当按下着几个键后,光标会变为一个十字,可以拖拉来选取拍报区域。 |
05 | 3)Command-Shift-Control-3(适用于OS9和10.2) |
06 | 将整个屏幕拍下并保存到剪贴板,可以Command+V直接粘贴到如Photoshop等软件中编辑。 |
07 | 4)Command-Shift-Control-4(适用于OS9和10.2) |
09 | 5)Command-Shift-4再按空格键(适用于10.2) |
10 | 光标会变成一个照相机,点击可拍下当前窗口或菜单或Dock以及图标等,只要将照相机移动到不用区域(有效区域会显示为浅蓝色)点击。 |
11 | 6)Command-Shift-Control-4再按空格键(适用于10.2) |
13 | 7)Command-Shift-Capslock-4(适用于OS9) |
15 | 8)Command-Shift-Capslock-Control-4(适用于OS9) |