您的位置:首页 > 移动开发 > IOS开发

[IOS]iphone开发之常用代码:不断更新

2011-12-05 09:29 513 查看
1,获取翻转事件,并开启翻转:

只要在viewcontroller的类中加入

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{

//翻转后要执行的代码

return YES;

}

2,-(void)viewWillAppear:(BOOL)animated,- (void)viewDidLoad 的区别。

viewwillappear是每次视图控制器的视图出现前执行的代码。而viewdidload是每次视图控制器载入是执行的代码。

比如说:当a视图控制器的视图第一次出现是两个都要执行,但当a被push后有pop回来时,只有viewwillappear执行。

3,如何让视图始终跟着手指移动,并有反弹事件

xsum=photopositon.origin.x+photopositon.size.width/2-touchstart.x;

ysum=photopositon.origin.y+photopositon.size.height/2-touchstart.y;

currentview.center=CGPointMake(xsum+p.x,
ysum+p.y);

if (pow(currentview.center.x-160,2.0)>pow(photopositon.size.width/2,2.0)||

pow(currentview.center.y-240,2.0)>pow(photopositon.size.height/2, 2.0))

{

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIView setAnimationDuration:0.3];

currentview.center=CGPointMake(160, 240);

[UIView commitAnimations];

}

就是让currentview的视图中心始终与手指保持一定的方位。

4,控制导航条和toolbar

[self.navigationController
...]

[self.navigationController.toolbar ...]

比如说让他们都消失:

[self.navigationController setToolbarItems:NULL animated:YES];

[self.navigationController.toolbar setBarStyle:UIBarStyleBlackTranslucent];

[self.navigationController setToolbarHidden:NO animated:YES];

5,自定义控件事件:

addTarget:self action:@selector(....) forControlEvents:....

比如获取某个按钮的触摸事件;

[new addTarget:self action:@selector(onChooseItem:) forControlEvents:UIControlEventTouchUpInside]

则当按钮按下时,- (void)onChooseItem:(id)
sender 就会被调用。

sender传的就是被按下按钮的指针。

6.获取文件的路径,即获取documents的路径

//获取文件路径
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath=[path objectAtIndex:0];

NSLog(@"%@",documentsPath);


补充:

1. NSSearchPathForDirectoriesInDomains和NSHomeDirectory
iPhone和symbian 3rd一样,会为每一个应用程序生成一个私有目录,这个目录位于/Users/sundfsun2009/Library/Application Support/iPhone         Simulator/User/Applications下,并随即生成一个数字字母串作为目录名,在每一次应用程序启动时,这个字母数字串都是不同于上一次。

通常使用Documents目录进行数据持久化的保存,而这个Documents目录可以通过 NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES) 得到,代码如下:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// NSString *path = [documentsDirectory stringByAppendingPathComponent:@"aa.plist"];

NSLog(@"path:   %@",path);

打印结果如下:

path:   /Users/apple/Library/Application Support/iPhone Simulator/4.3/Applications/550AF26D-174B-42E6-881B-B7499FAA32B7/Documents

而这个目录还可以通过 NSHomeDirectory()来得到,代码如下:

NSString *destPath = NSHomeDirectory();
NSLog(@"path:   %@",destPath);
//destPath = [destPath stringByAppendingPathComponent: @"Documents"];
//NSString *xmlpath = [destPath stringByAppendingPathComponent: @"menu/menu.xml"];

打印结果如下:

path:   /Users/apple/Library/Application Support/iPhone Simulator/4.2/Applications/6F4BC466-C5D6-440C-BAAC-BE20FA468C61

看看两者打印出来的结果,我们可以看出这两种方法的不同。

2. 浏览document下所有图片资源

#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
NSArray *fileList = [[[NSFileManager defaultManager] directoryContentsAtPath:DOCUMENTS_FOLDER]
pathsMatchingExtensions:[NSArray arrayWithObject:@"png"]] ;

3. 得到图片中的某一部分:

UIImage *image = [UIImage imageNamed:filename];
CGImageRef imageRef = image.CGImage;

CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height);

CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);

UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];


7.在documents的路径下创建文件。

首先要获取documents的路径,如上第6条。

其次就是下面的语句了:

NSString *writePath=[NSString stringWithFormat:@"%@/%@.txt",documentsPath,@"aaa"];
NSData *data = [@"" dataUsingEncoding:NSUTF8StringEncoding];//新文件的初始数据,设为空
[[NSFileManager defaultManager] createFileAtPath:writePath contents:data attributes:nil];//创建文件的命令在这里


这样就可以在documents文件夹下创建一个aaa.txt的文件了。哈哈哈哈哈。

或者是用下面的语句,

NSString *writePath=[NSString stringWithFormat:@"%@/%@.txt",documentsDirectory,@"bbb"];
NSError *error;
[@"fasdfasdasddaa" writeToFile:writePath atomically:YES encoding:NSUTF8StringEncoding error:&error];


这样就可以在documents文件夹下创建一个bbb.txt的文件了。并且,文件中的有"fasdfasdasddaa"字符。

总结起来就是,先给要存储的东西取一个名字,然后,找到它的路径。然后以这个名字创建。创建的时候可以添加内容。

当然,图片也可以批量的生成,假如说让你把一张图片复制500遍,并且给他按照(1-500)重命名。

你可以用上面的方法,用一个循环批量的生成500张图片。

UIImage *image = [UIImage imageNamed:@"240*360.png"];
NSData *data = UIImagePNGRepresentation(image);
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSMutableArray *muArray;
NSString *filePath = nil;
for (int j = 1; j<=500; j++) {
filePath = [[NSString alloc] initWithFormat:@"%@/%d.png",documentPath,j];
[data writeToFile:filePath atomically:YES];
NSString *newPath = [[NSString alloc] initWithFormat:@"%@",documentPath];
muArray = [[NSMutableArray alloc] initWithContentsOfFile:newPath];
[muArray addObject:filePath];
[filePath release];



7.iphone开发中随机数的产生。

// Get random number between 0 and 99
int x = arc4random() % 81;
// Get random number between 500 and 999
int y = ((arc4random()%501)+500);

NSLog(@"0--99之间的随机数%d",x);
NSLog(@"500--999之间的随机数%d",y);


8.好看的文字处理

以tableView中cell的textLabel为例子:

cell.backgroundColor  = [UIColor scrollViewTexturedBackgroundColor];
//设置文字的字体
cell.textLabel.font = [UIFont fontWithName:@"American Typewriter" size:100.0f];
//设置文字的颜色
cell.textLabel.textColor = [UIColor orangeColor];
//设置文字的背景颜色
cell.textLabel.shadowColor = [UIColor whiteColor];
//设置文字的显示位置
cell.textLabel.textAlignment = UITextAlignmentCenter;


原文链接:http://www.cnblogs.com/iphone520/archive/2011/11/28/2225160.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: