object-c 常见示例
来源:互联网 发布:string数组增加元素 编辑:程序博客网 时间:2024/05/17 04:35
记录:
在Xcode,单击Run>Console查看NSLog语句。
NSLog(@"log: %@ ", myString);
NSLog(@"log: %f ", myFloat);
NSLog(@"log: %i ", myInt);
显示图片:
不使用UIBuilder,在屏幕任意处显示图片。你也能够看到其他类型的视图。
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];[myImage setImage:[UIImage imageNamed:@"myImage.png"]];myImage.opaque = YES; // explicitly opaque for performance[self.view addSubview:myImage];[myImage release];
web视图:
一个基本的UIWebView。
CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];[webView setBackgroundColor:[UIColor whiteColor]];NSString *urlAddress = @"http://www.google.com";NSURL *url = [NSURL URLWithString:urlAddress];NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];[webView loadRequest:requestObj];[self addSubview:webView]; [webView release];
显示网络活动状态指示灯:
这是iphone左上角状态显示的网络状态图标。
UIApplication* app = [UIApplication sharedApplication];app.networkActivityIndicatorVisible = YES; // to stop it, set this to NO
动画:序列图
显示一系列图片。
NSArray *myImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"myImage1.png"], [UIImage imageNamed:@"myImage2.png"], [UIImage imageNamed:@"myImage3.png"], [UIImage imageNamed:@"myImage4.gif"], nil];
UIImageView *myAnimatedView = [UIImageView alloc];[myAnimatedView initWithFrame:[self bounds]];myAnimatedView.animationImages = myImages;myAnimatedView.animationDuration = 0.25; // secondsmyAnimatedView.animationRepeatCount = 0; // 0 = loops forever[myAnimatedView startAnimating];[self addSubview:myAnimatedView];[myAnimatedView release];
动画:移动对象
显示在屏幕移动的东西,注:这种动画是“懒惰”的,你不能从这动画中获得任何对象(例如当前的位置),如果你需要这些信息,你需要用NSTimer手动控制其x,y坐标。
CABasicAnimation *theAnimation; theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];theAnimation.duration=1;theAnimation.repeatCount=2;theAnimation.autoreverses=YES;theAnimation.fromValue=[NSNumber numberWithFloat:0];theAnimation.toValue=[NSNumber numberWithFloat:-60];[view.layer addAnimation:theAnimation forKey:@"animateLayer"];
字符串和整型:
下面用一个文本标签显示一个整型的值。
currentScoreLabel.text = [NSString stringWithFormat:@"%d", currentScore];
可拖动的项:
这里介绍怎样创建一个可拖动的图片。
1.创建一个继承于UIImageView的类。
@interface myDraggableImage : UIImageView {}
2.在实现文件中,增加两个方法。
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { // Retrieve the touch point CGPoint pt = [[touches anyObject] locationInView:self]; startLocation = pt; [[self superview] bringSubviewToFront:self]; }- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { // Move relative to the original touch point CGPoint pt = [[touches anyObject] locationInView:self]; CGRect frame = [self frame]; frame.origin.x += pt.x - startLocation.x; frame.origin.y += pt.y - startLocation.y; [self setFrame:frame];}
3.现在你可以在任何新的图像中增加此功能了。
dragger = [[myDraggableImage alloc] initWithFrame:myDragRect];[dragger setImage:[UIImage imageNamed:@"myImage.png"]];[dragger setUserInteractionEnabled:YES];
振动与声音:
这里是如何使手机振动。
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
声音一般是可以在模拟器中模拟的,但有些声音是无法在模拟器中模拟或是需要特定的音频格式。
SystemSoundID pmph;id sndpath = [[NSBundle mainBundle] pathForResource:@"mySound" ofType:@"wav" inDirectory:@"/"];CFURLRef baseURL = (CFURLRef) [[NSURL alloc] initFileURLWithPath:sndpath];AudioServicesCreateSystemSoundID (baseURL, &pmph);AudioServicesPlaySystemSound(pmph); [baseURL release];
线程:
1.创建新线程。
[NSThread detachNewThreadSelector:@selector(myMethod) toTarget:self withObject:nil];
2.创建线程调用的方法。
- (void)myMethod { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; *** code that should be run in the new thread goes here *** [pool release];}
如果你需要在主线程中做一些事情,你可以使用performSelectorOnMainThread.
[self performSelectorOnMainThread:@selector(myMethod) withObject:nil waitUntilDone:false];
在其他类中访问属性/方法:
其他一个方法是通过AppDelegate。
myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];[[[appDelegate rootViewController] flipsideViewController] myMethod];
随机数:
可以使用arc4random(),random() ,但要手动实现,而应该首选arc4random()。
计时器:
计时器能够每隔一秒调用我们的方法。
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myMethod) userInfo:nil repeats:YES];
如果你想传递一个对象到myMethod,请使用userInfo属性。
1.首先创建一个Timer。
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myMethod) userInfo:myObject repeats:YES];
2.传递计时器对象到你的方法。
-(void)myMethod:(NSTimer*)timer { // Now I can access all the properties and methods of myObject [[timer userInfo] myObjectMethod];}
3.停止计时器用invalidate。
[myTimer invalidate];myTimer = nil; // ensures we never invalidate an already invalid Timer
时间:
用CFAbsoluteTimeGetCurrent()来获取时间。
CFAbsoluteTime myCurrentTime = CFAbsoluteTimeGetCurrent();// perform calculations here
提示:
显示一个带有OK按钮的提示框。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"An Alert!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];[alert show];[alert release];
plist文件:
plist文件能够储存在程序程序中的资源文件夹,当应用程序首次启动时,会自动检测用户文件夹是否存在plist文件,如果没有,应该在应用程序包中复制一份。
// Look in Documents for an existing plist fileNSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];myPlistPath = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.plist", plistName] ];[myPlistPath retain];
// If it's not there, copy it from the bundleNSFileManager *fileManger = [NSFileManager defaultManager];if ( ![fileManger fileExistsAtPath:myPlistPath] ) { NSString *pathToSettingsInBundle = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];}
现在从文档中读取plist文件。
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectoryPath = [paths objectAtIndex:0];NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:@"myApp.plist"];NSMutableDictionary *plist = [NSDictionary dictionaryWithContentsOfFile: path];
现在读取并设置键值。
myKey = (int)[[plist valueForKey:@"myKey"] intValue];myKey2 = (bool)[[plist valueForKey:@"myKey2"] boolValue];[plist setValue:myKey forKey:@"myKey"];[plist writeToFile:path atomically:YES];
信息按钮:
为了容易触摸,增加按钮的范围。
CGRect newInfoButtonRect = CGRectMake(infoButton.frame.origin.x-25, infoButton.frame.origin.y-25, infoButton.frame.size.width+50, infoButton.frame.size.height+50);[infoButton setFrame:newInfoButtonRect];
检测子视图:
你可以循环测试子视图,如果使用了标签属性,这是很好用的。
for (UIImageView *anImage in [self.view subviews]) { if (anImage.tag == 1) { // do something }}
- object-c 常见示例
- Object-c 常见错误
- object-c 常见输出格式
- object-c中NSString的常见操作
- object-c常见的知识点-1
- 黑马程序员——Object-C基础(八)Object-C常见特性
- Object参数示例
- java Object使用示例
- Object类toString示例
- Object-c Associated Object
- 常见对象-Object类
- 常见类---Object类
- Object常见方法
- object-c
- Object-C
- Object-c
- object-c
- object c
- svn myEclipse插件的使用
- 黑马程序员 io
- form表单button自动提交表单处理方法
- 黑马程序员 网络编程
- linux下采用trap对信号进行捕捉
- object-c 常见示例
- 12--4猜数字游戏
- android游戏开发视频教程下载
- Extjs4中的Form之radiogroup和checkboxgroup
- android中dip、dp、px、sp和屏幕密度
- 第十三周项目4--最大公约数(非递归)
- 1176 魔幻数字47
- intel nvidia双显卡设置(亲测可行)
- 黑马程序员 正则