iOS每日一记————————一些常用的小技巧(三)

Quartz 中如何设置旋转点

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImageimageNamed:@"bg.png"]];

imageView.layer.anchorPoint = CGPointMake(0.5, 1.0);这个是把旋转点设置为底部中间。记住是在 QuartzCore.framework 中才得到支

创建.plist 文件并存储

NSString *errorDesc; //用来存放错误信息


NSMutableDictionary *rootObj = [NSMutableDictionarydictionaryWithCapacity:4]; //NSDictionary, NSData 等文件可以直接转化为 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 name
innerDict = [self getAllNodeInfoToDictionary:player];

[rootObj setObject:innerDict forKey:name]; // This "Player1" denotes theperson who start this game

}
player = nil;NSData

dataFromPropertyList:(id)rootObjerrorDescription:&errorDesc];

*plistData

= [NSPropertyListSerializationformat:NSPropertyListXMLFormat_v1_0



红色部分可以忽略,只是给 rootObj 添加一点内容。这个 plistData 为创建好的plist 文件,用其 writeToFile 方法就可以写成文件。下面是代码:

/*得到移动设备上的文件存放位置*/
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];

}



读取一般性文档文件


NSString *tmp;
NSArray *lines; /*
将文件转化为一行一行的*/
lines = [[NSString stringWithContentsOfFile:@"testFileReadLines.txt"]

componentsSeparatedByString:@"\n"];NSEnumerator *nse = [lines objectEnumerator];


// 读取<>里的内容
while(tmp = [nse nextObject]) {

}

= [[NSMutableDictionary

alloc]

NSString *stringBetweenBrackets = nil;
NSScanner *scanner = [NSScanner scannerWithString:tmp];[scanner scanUpToString:@"<" intoString:nil];
[scanner scanString:@"<" intoString:nil];
[scanner scanUpToString:@">" intoString:&stringBetweenBrackets];

NSLog([stringBetweenBrackets description]);

}



隐藏 NavigationBar
[self.navigationController setNavigationBarHidden:YES animated:YES];



如何在 iPhone 程序读取数据时显示进度窗
下面代码说明如何使用 iPhone 非官方 SDK 在读取数据时显示进度条。以下代码参考了 MobileRss
定义头文件:
#import "uikit/UIProgressHUD.h"

@interface EyeCandy : UIApplication {UIProgressHUD *progress;

}

- (void) showProgressHUD:(NSString *)label withWindow:(UIWindow *)wwithView:(UIView *)v withRect:(struct CGRect)rect;

- (void) hideProgressHUD;.@end


上面的引号要改成<>

import "EyeCandy.h"

@implementation EyeCandy

- (void)showProgressHUD:(NSString *)label withWindow:(UIWindow *)wwithView:(UIView *)v withRect:(struct CGRect)rect

{
progress = [[UIProgressHUD alloc] initWithWindow: w];[progress setText: label];
[progress drawRect: rect];
[progress show: YES];

[v addSubview:progress];}

- (void)hideProgressHUD{

[progress show: NO];

[progress removeFromSuperview];}

@end

使用下面代码调用:

// Setup Eye Candy View
_eyeCandy = [[[EyeCandy alloc] init] retain];

// Call loading display

[_eyeCandy showProgressHUD:@"Loading ..." withWindow:window withView:mainViewwithRect:CGRectMake(0.0f, 100.0f, 320.0f, 50.0f)];

// When finished for hiding the &quot;loading text&quot;[_eyeCandy hideProgressHUD];


NSNotificationCenter 带参数发送

MPMoviePlayerController* theMovie = [[MPMoviePlayerControlleralloc]initWithContentURL:[NSURL fileURLWithPath:[[[tableTitles objectForKey:keyIndex]objectAtIndex:row] objectAtIndex:3] ]];

[[NSNotificationCenter defaultCenter]selector:@selector(myMovieFinishedCallback:)name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];

[theMovie play];-(void)myMovieFinishedCallback:(NSNotification*)aNotification{

MPMoviePlayerController *theMovie = [aNotification object];

[[NSNotificationCenter defaultCenter]name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];

// Release the movie instance [theMovie release];}

  ------------

addObserver:self

MPMoviePlayerController* theMovie = [[MPMoviePlayerControlleralloc]initWithContentURL:[NSURL fileURLWithPath:[[[tableTitles objectForKey:keyIndex]

removeObserver:self


objectAtIndex:row] objectAtIndex:3] ]];

[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(myMovieFinishedCallback:)name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie userInfo:dic];

[theMovie play];

-(void)myMovieFinishedCallback:(NSNotification*)aNotification

{

MPMoviePlayerController *theMovie = [aNotification object];

[[NSNotificationCenter defaultCenter] removeObserver:selfname:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];

// Release the movie instance [theMovie release];
}


获取 IOS 设备的基本信息系统唯一标识
是什么设备:
iPad 还是 iPhone iOS 版本号

系统名称


[[UIDevice currentDevice] uniqueIdentifier],


[[UIDevice currentDevice] localizedModel],[[UIDevice currentDevice] systemVersion],[[UIDevice currentDevice] systemName],[[UIDevice currentDevice] model]];



NSDateFormatter 调整时间格式的代码

在开发 iOS 程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用 NSDateFormatter 类来处理。

例如:

//实例化一个 NSDateFormatter 对象

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//设定时间格式,这里可以设置成自己需要的格式

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

//[NSDate date]可以获取系统当前时间

NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];

//输出格式为:2010-10-27 10:22:13

NSLog(@”%@”,currentDateStr);

//alloc 后对不使用的对象别忘了 release

[dateFormatter release];


UIView 设置成圆角方法

m_mainImgView.layer.cornerRadius = 6;m_mainImgView.layer.masksToBounds = YES;


iphone 更改键盘右下角按键的 type


创建 mySearchBar:

mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0,SEARCH_HEIGHT)];

mySearchBar.placeholder = curPath;[mySearchBar setDelegate:self];//tableView.tableHeaderView =mySearchBar;[self.view addSubview:mySearchBar];

0,320,

更改按键的 keyType(默认是 return,这里将它更改成 done,当然还可以更改成其他的):UITextField *searchField = [[mySearchBar subviews] lastObject];
[searchField setReturnKeyType:UIReturnKeyDone];
[mySearchBar release];






你可能感兴趣的:(ios)