iphone开发常用代码

更改cell选中的背景

 

Java代码   收藏代码
  1. //创建一个UIView对象   
  2. UIView *myview = [[UIView alloc] init];  
  3. //设置UIView对象的外观大小   
  4. myview.frame = CGRectMake(0032047);   
  5. //设置UIView对象的背景色。 [UIColor colorWithPatternImage:[UIImage imageNamed:@"0006.png"]] 从图片中创建颜色   
  6.   
  7. myview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"0006.png"]];   
  8. //设置cell被选中时的颜色   
  9. cell.selectedBackgroundView = myview;   

 
 

在数字键盘上添加button:
 
 

Java代码   收藏代码
  1. //定义一个消息中心   
  2. //addObserver:注册一个观察员 name:消息名称   
  3. [[NSNotificationCenter defaultCenter]   
  4.   addObserver:self   
  5.    selector:@selector(keyboardWillShow:)   
  6.     name:UIKeyboardWillShowNotification object:nil];   
  7. //定义的动作   
  8. - (void)keyboardWillShow:(NSNotification *)note {   
  9. // 创建一个自定义的UIButton对象   
  10. UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];   
  11. //设置UIButton大小的外观大小  
  12. doneButton.frame = CGRectMake(016310653);   
  13. //设置该UIButton对象在正常状态下显示的图片   
  14. [doneButton setImage:[UIImage imageNamed:@"5.png"] forState:UIControlStateNormal];   
  15. //注册事件   
  16. [doneButton addTarget:self action:@selector(addRadixPoint) forControlEvents:UIControlEventTouchUpInside];  
  17.  // locate keyboard view取得UIWindow对象   
  18. UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];UIView* keyboard;   
  19. //遍历window上的所有subview   
  20.   for(int i=0; i<[tempWindow.subviews count]; i++)   
  21.     {   
  22.        keyboard = [tempWindow.subviews objectAtIndex:i];   
  23. // keyboard view found; add the custom button to it //如果该subview是键盘,就在该键盘上添加创建的button 对象   
  24.        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)   
  25.   [keyboard addSubview:doneButton];   
  26.     }   
  27. }   
  28.    

 
 

正则表达式使用:


    被用于正则表达式的字串必须是可变长的,不然会出问题

 

 


将一个空间放在视图之上.above:在什么之上的意思

Java代码   收藏代码
  1.    
  2. [scrollView insertSubview:searchButton aboveSubview:scrollView];   

 
 
 
 
 
从本地加载图片

Java代码   收藏代码
  1. //取得当前应用程序的束--[NSBundle mainBundle]   
  2. //取得资源的路径   
  3. NSString *boundle = [[NSBundle mainBundle] resourcePath];  
  4. [web1 loadHTMLString:[NSString stringWithFormat:@"<img src='0001.png'/>"]   
  5.  baseURL:[NSURL fileURLWithPath:boundle]];   
  6.    

 
 
 
 
  
从网页加载图片并让图片在规定长宽中缩小

 

Java代码   收藏代码
  1. [cell.img   loadHTMLString:  
  2.           [NSString stringWithFormat:@"<html><body><img src='%  @'height='90px'width='90px'></body></html>",goodsInfo.GoodsImg]   
  3.    baseURL:nil  
  4.   ];  
  5.    

 
 
将网页加载到webview上通过javascript获取里面的数据,
如果只是发送了一个连接请求获取到源码以后可以用正则表达式进行获取数据

C代码   收藏代码
  1. 1. NSString *javaScript1 = @"document.getElementsByName('.u').item(0).value";     
  2. 2. NSString *javaScript2 = @"document.getElementsByName('.challenge').item(0).value";     
  3. 3. NSString *strResult1 = [NSString stringWithString:[theWebView stringByEvaluatingJavaScriptFromString:javaScript1]];     
  4. 4. NSString *strResult2 = [NSString stringWithString:[theWebView stringByEvaluatingJavaScriptFromString:javaScript2]];    

 
 


用NSString怎么把UTF8转换成unicode

 

Java代码   收藏代码
  1. utf8Str //  
  2. NSString *unicodeStr = [NSString stringWithCString:[utf8Str UTF8String] encoding:NSUnicodeStringEncoding];  

 
View自己调用自己的方法:

 

Java代码   收藏代码
  1. //黄色段为方法名,和延迟几秒执行.loginToNext:方法名称 。afterDelay:2--延长2秒调用  
  2. [self performSelector:@selector(loginToNext) withObject:nil afterDelay:2];  

 
显示图像:

 

Java代码   收藏代码
  1. CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);  
  2. UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];  
  3. [myImage setImage:[UIImage imageNamed:@"myImage.png"]];  
  4. //opaque是否透明  
  5. myImage.opaque = YES;   
  6. [self.view addSubview:myImage];  
  7. [myImage release];  

 

WebView:

 

Java代码   收藏代码
  1. CGRect webFrame = CGRectMake(0.00.0320.0460.0);  
  2. UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];  
  3. [webView setBackgroundColor:[UIColor whiteColor]];  
  4. NSString *urlAddress = @"http://www.google.com";  
  5. NSURL *url = [NSURL URLWithString:urlAddress];  
  6. NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];  
  7. [webView loadRequest:requestObj];  
  8. [self addSubview:webView];  
  9. [webView release];  

 

 

 

显示网络活动状态指示符
这是在iPhone左上部的状态栏显示的转动的图标指示有背景发生网络的活动。

 

Java代码   收藏代码
  1. UIApplication* app = [UIApplication sharedApplication];  
  2. app.networkActivityIndicatorVisible = YES;  

 
动画:一个接一个地显示一系列的图象

Java代码   收藏代码
  1. NSArray *myImages = [NSArray   
  2.    arrayWithObjects:   
  3.       [UIImage imageNamed:@"myImage1.png"],  
  4.       [UIImage imageNamed:@"myImage2.png"],  
  5.                                          [UIImage imageNamed:@"myImage3.png"],  
  6.       [UIImage imageNamed:@"myImage4.gif"],  
  7.      nil];  
  8.   
  9. //创建一个UIImageView对象  
  10. UIImageView *myAnimatedView = [UIImageView alloc];  
  11. //初始化UIImageView对象的大小  
  12. [myAnimatedView initWithFrame:[self bounds]];  
  13.   
  14. //animationImages属性返回一个存放动画图片的数组  
  15. myAnimatedView.animationImages = myImages;  
  16. //浏览整个图片一次所用的时间   
  17. myAnimatedView.animationDuration = 0.25;   
  18. // 0 = loops forever 动画重复次数  
  19. myAnimatedView.animationRepeatCount = 0;   
  20.   
  21. //开始动画  
  22. [myAnimatedView startAnimating];  
  23. //把该UIImageView对象添加到view视图中  
  24. [self addSubview:myAnimatedView];  
  25. //释放  
  26. [myAnimatedView release];  

 


动画:显示了something在屏幕上移动。
注:这种类型的动画是“开始后不处理” -你不能获取任何有关物体在动画中的信息(如当前的位置) 。
如果您需要此信息,您会手动使用定时器去调整动画的X和Y坐标
这个需要导入QuartzCore.framework

Java代码   收藏代码
  1. CABasicAnimation *theAnimation;  
  2.    theAnimation=  
  3.     [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];  
  4.   
  5. //Creates and returns an CAPropertyAnimation instance for the specified key path.  
  6. //parameter:the key path of the property to be animated  
  7.    theAnimation.duration=1;  
  8.    theAnimation.repeatCount=2;  
  9.    theAnimation.autoreverses=YES;  
  10.    theAnimation.fromValue=[NSNumber numberWithFloat:0];  
  11.    theAnimation.toValue=[NSNumber numberWithFloat:-60];  
  12.     
  13.   [view.layer addAnimation:theAnimation forKey:@"animateLayer"];  

 

 

 

Draggable items//拖动项目

Here's how to create a simple draggable image.//这是如何生成一个简单的拖动图象

Java代码   收藏代码
  1. 1. Create a new class that inherits from UIImageView  
  2.   
  3.    @interface myDraggableImage : UIImageView   
  4.                       {   
  5.                            }  
  6.   
  7. 2. In the implementation for this new class, add the 2 methods:  
  8.   
  9.    - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event  
  10.       {  
  11.    // Retrieve the touch point 检索接触点  
  12.    CGPoint pt = [[touches anyObject] locationInView:self];  
  13.    startLocation = pt;  
  14.    [[self superview] bringSubviewToFront:self];  
  15.       }  
  16.   
  17.   
  18.   - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event  
  19.  {  
  20.     // Move relative to the original touch point 相对以前的触摸点进行移动  
  21.     CGPoint pt = [[touches anyObject] locationInView:self];  
  22.     CGRect frame = [self frame];  
  23.     frame.origin.x += pt.x - startLocation.x;  
  24.     frame.origin.y += pt.y - startLocation.y;  
  25.     [self setFrame:frame];  
  26.  }  
  27.   
  28. 3. Now instantiate the new class as you would any other new image and add it to your view  
  29. //实例这个新的类,放到你需要新的图片放到你的视图上  
  30.   
  31.    dragger = [[myDraggableImage alloc] initWithFrame:myDragRect];  
  32.     [dragger setImage:[UIImage imageNamed:@"myImage.png"]];  
  33.     [dragger setUserInteractionEnabled:YES];  

 

线程:

 

Java代码   收藏代码
  1. 1. Create the new thread:  
  2.   
  3.  [NSThread detachNewThreadSelector:@selector(myMethod)toTarget:self withObject:nil];  
  4.   
  5. 2. Create the method that is called by the new thread:  
  6.   
  7.  - (void)myMethod  
  8.  {  
  9.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  10.  *** code that should be run in the new thread goes here ***  
  11.     [pool release];  
  12.  }  
  13.   
  14.    
  15.   
  16. /***What if you need to do something to the main thread from   inside your new thread  
  17. *(for example, show a loading //symbol)? Use  *performSelectorOnMainThread.  
  18. **/  
  19.   
  20.    [self performSelectorOnMainThread:@selector(myMethod)withObject:nil waitUntilDone:false];  
  21.   
  22.   
  23.   
  24.   
  25.    
  26.   
  27.    

 

 

 

// Look in Documents for an existing plist file

 

 

 

Java代码   收藏代码
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);  
  2.    NSString *documentsDirectory = [paths objectAtIndex:0];  
  3.    myPlistPath = [documentsDirectory stringByAppendingPathComponent:  
  4.         [NSString   
  5.          stringWithFormat:  
  6.             @"%@.plist",  
  7.                   plistName]  
  8.      ];  
  9.    [myPlistPath retain];  

 

 

 

// If it's not there, copy it from the bundle

 

Java代码   收藏代码
  1.  NSFileManager *fileManger = [NSFileManager defaultManager];  
  2.   if ( ![fileManger fileExistsAtPath:myPlistPath] )  
  3. {  
  4.   NSString *pathToSettingsInBundle = [[NSBundle mainBundle]   
  5.       pathForResource:plistName   
  6.       ofType:  
  7.        @"plist"];  
  8. }  

  

 

 

 

//Now read the plist file from Documents

 

Java代码   收藏代码
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);  
  2. NSString *documentsDirectoryPath = [paths objectAtIndex:0];  
  3. NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:@"myApp.plist"];  
  4. NSMutableDictionary *plist = [NSDictionary dictionaryWithContentsOfFile: path];  

 

 

//Now read and set key/values

 

Java代码   收藏代码
  1. myKey = (int)[[plist valueForKey:@"myKey"] intValue];  
  2.  myKey2 = (bool)[[plist valueForKey:@"myKey2"] boolValue];  
  3.  [plist setValue:myKey forKey:@"myKey"];  
  4.  [plist writeToFile:path atomically:YES];  

  

 

//Alerts Show a simple alert with OK button.

 

Java代码   收藏代码
  1.   UIAlertView *alert = [[UIAlertView alloc]   initWithTitle:nil   message: @"An Alert!"  delegate:self  cancelButtonTitle:@"OK" otherButtonTitles:nil ];  
  2. [alert show];  
  3. [alert release];  

  

 

//Info button
//Increase the touchable area on the Info button, so it's easier to press.

 

Java代码   收藏代码
  1. CGRect newInfoButtonRect = CGRectMake(infoButton.frame.origin.x-25,  infoButton.frame.origin.y-25,infoButton.frame.size.width+50,infoButton.frame.size.height+50);  
  2. [infoButton setFrame:newInfoButtonRect];  

 

 

 

//Detecting Subviews
//You can loop through subviews of an existing view. This works especially well if you use the "tag"

 property on your views.

 

Java代码   收藏代码
  1. for (UIImageView *anImage in [self.view subviews])  
  2.  {  
  3.   
  4.  if (anImage.tag == 1)  
  5.                   {   
  6.           // do something  
  7.   
  8.               }  
  9.   
  10.  }  

  

转自:http://o0o0o0o.iteye.com/blog/623459

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值