`
yangping_Account
  • 浏览: 187268 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

iPhone开发笔记(一)

阅读更多
更改cell选中的背景

//创建一个UIView对象 
UIView *myview = [[UIView alloc] init];
//设置UIView对象的外观大小 
myview.frame = CGRectMake(0, 0, 320, 47); 
//设置UIView对象的背景色。 [UIColor colorWithPatternImage:[UIImage imageNamed:@"0006.png"]] 从图片中创建颜色 myview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"0006.png"]]; 
//设置cell被选中时的颜色 
cell.selectedBackgroundView = myview; 
 

在数字键盘上添加button:
 
 
 
//定义一个消息中心 
//addObserver:注册一个观察员 name:消息名称 
[[NSNotificationCenter defaultCenter] 
		addObserver:self 
			selector:@selector(keyboardWillShow:) 
				name:UIKeyboardWillShowNotification object:nil]; 
//定义的动作 
- (void)keyboardWillShow:(NSNotification *)note { 
// 创建一个自定义的UIButton对象 
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
//设置UIButton大小的外观大小
doneButton.frame = CGRectMake(0, 163, 106, 53); 
//设置该UIButton对象在正常状态下显示的图片 
[doneButton setImage:[UIImage imageNamed:@"5.png"] forState:UIControlStateNormal]; 
//注册事件 
[doneButton addTarget:self action:@selector(addRadixPoint) forControlEvents:UIControlEventTouchUpInside];
 // locate keyboard view取得UIWindow对象 
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];UIView* keyboard; 
//遍历window上的所有subview 
  for(int i=0; i<[tempWindow.subviews count]; i++) 
    { 
       keyboard = [tempWindow.subviews objectAtIndex:i]; 
// keyboard view found; add the custom button to it //如果该subview是键盘,就在该键盘上添加创建的button 对象 
       if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
		[keyboard addSubview:doneButton]; 
    } 
} 
 
正则表达式使用:
被用于正则表达式的字串必须是可变长的,不然会出问题


将一个空间放在视图之上.above:在什么之上的意思
 
[scrollView insertSubview:searchButton aboveSubview:scrollView]; 
 
 
 
 
从本地加载图片

//取得当前应用程序的束--[NSBundle mainBundle] 
//取得资源的路径 
NSString *boundle = [[NSBundle mainBundle] resourcePath];
[web1 loadHTMLString:[NSString stringWithFormat:@"<img src='0001.png'/>"] 
	baseURL:[NSURL fileURLWithPath:boundle]]; 
 
 
 
 
  
从网页加载图片并让图片在规定长宽中缩小 

[cell.img 
   loadHTMLString:
	[NSString stringWithFormat:@"<html><body><img src='%@'height='90px'width='90px'></body></html>",goodsInfo.GoodsImg] 
   baseURL:nil
  ];
 
 
将网页加载到webview上通过javascript获取里面的数据,
如果只是发送了一个连接请求获取到源码以后可以用正则表达式进行获取数据
  
1.	NSString *javaScript1 = @"document.getElementsByName('.u').item(0).value";   
2.	NSString *javaScript2 = @"document.getElementsByName('.challenge').item(0).value";   
3.	NSString *strResult1 = [NSString stringWithString:[theWebView stringByEvaluatingJavaScriptFromString:javaScript1]];   
4.	NSString *strResult2 = [NSString stringWithString:[theWebView stringByEvaluatingJavaScriptFromString:javaScript2]];  
 


用NSString怎么把UTF8转换成unicode

utf8Str //
NSString *unicodeStr = [NSString stringWithCString:[utf8Str UTF8String] encoding:NSUnicodeStringEncoding];


View自己调用自己的方法:

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


显示图像:

CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
//opaque是否透明
myImage.opaque = YES; 
[self.view addSubview:myImage];
[myImage release];



WebView:

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;


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

NSArray *myImages = [NSArray 
			arrayWithObjects: 
					 [UIImage imageNamed:@"myImage1.png"],
					 [UIImage imageNamed:@"myImage2.png"],
                                         [UIImage imageNamed:@"myImage3.png"],
					 [UIImage imageNamed:@"myImage4.gif"],
				 nil];

//创建一个UIImageView对象
UIImageView *myAnimatedView = [UIImageView alloc];
//初始化UIImageView对象的大小
[myAnimatedView initWithFrame:[self bounds]];

//animationImages属性返回一个存放动画图片的数组
myAnimatedView.animationImages = myImages;
//浏览整个图片一次所用的时间 
myAnimatedView.animationDuration = 0.25; 
// 0 = loops forever 动画重复次数
myAnimatedView.animationRepeatCount = 0; 

//开始动画
[myAnimatedView startAnimating];
//把该UIImageView对象添加到view视图中
[self addSubview:myAnimatedView];
//释放
[myAnimatedView release];




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

CABasicAnimation *theAnimation;
		 theAnimation=
				[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];

//Creates and returns an CAPropertyAnimation instance for the specified key path.
//parameter:the key path of the property to be animated
   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"];



Draggable items//拖动项目

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

1. Create a new class that inherits from UIImageView

   @interface myDraggableImage : UIImageView 
                      { 
                           }

2. In the implementation for this new class, add the 2 methods:

   - (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. Now instantiate the new class as you would any other new image and add it to your view
//实例这个新的类,放到你需要新的图片放到你的视图上

    dragger = [[myDraggableImage alloc] initWithFrame:myDragRect];
    [dragger setImage:[UIImage imageNamed:@"myImage.png"]];
    [dragger setUserInteractionEnabled:YES];



线程:

1. Create the new thread:

	[NSThread detachNewThreadSelector:@selector(myMethod) toTarget:self withObject:nil];

2. Create the method that is called by the new thread:

	- (void)myMethod
	{
	   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	*** code that should be run in the new thread goes here ***
	   [pool release];
	}

/***What if you need to do something to the main thread from inside your new thread 
 *(for example, show a loading //symbol)? Use  *performSelectorOnMainThread. 
**/

   [self performSelectorOnMainThread:@selector(myMethod) withObject:nil waitUntilDone:false];


/*** Plist files Application-specific plist files can be stored in the Resources folder of the app bundle. 
 *When the app first launches, it should check if there is an existing plist in the user's Documents folder, 
 *and if not it should copy the plist from the app bundle.
**/
// Look in Documents for an existing plist file

   NSArray *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 bundle

  NSFileManager *fileManger = [NSFileManager defaultManager];
   if ( ![fileManger fileExistsAtPath:myPlistPath] )
	{
	  NSString *pathToSettingsInBundle = [[NSBundle mainBundle] 
							pathForResource:plistName 
							ofType:
								@"plist"];
	}

//Now read the plist file from Documents

	NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectoryPath = [paths objectAtIndex:0];
	NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:@"myApp.plist"];
	NSMutableDictionary *plist = [NSDictionary dictionaryWithContentsOfFile: path];

//Now read and set key/values

	myKey = (int)[[plist valueForKey:@"myKey"] intValue];
	myKey2 = (bool)[[plist valueForKey:@"myKey2"] boolValue];
	[plist setValue:myKey forKey:@"myKey"];
	[plist writeToFile:path atomically:YES];

//Alerts Show a simple alert with OK button.

  	UIAlertView *alert = [[UIAlertView alloc] 
 						initWithTitle:nil 
						 	message: @"An Alert!" 
						 	delegate:self 
						 	cancelButtonTitle:@"OK" 
						 	otherButtonTitles:nil
									];
	[alert show];
	[alert release];

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

	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];


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

	for (UIImageView *anImage in [self.view subviews])
		{
		if (anImage.tag == 1)
                   { 
			// do something
		   }
		}

正则表达式
NSString *mystring = @"Letter1234";
NSString *regex = @"[a-z][A-Z][0-9]



";

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

if ([predicate evaluateWithObject:mystring] == YES) {
//implement
}


分享到:
评论

相关推荐

    iPhone开发笔记

    自用的iPhone开发笔记备份-2012/3/31

    iphone开发笔记

    延时一段时间执行某一函数 42 无99美金证书联机开发 42 获取IOS设备的基本信息 42 用NSDateFormatter调整时间格式的代码 42 UIView设置成圆角方法 43 iPhone里的frame和bounds区别 43 Objective-C内存管理 44 iphone...

    iphone开发资料

    iphone开发笔记.iphone开发笔记.iphone开发笔记.iphone开发笔记.

    iPhone 开发笔记实录(代码讲解)

    收集了IOS开发过程中常见的错误和需要注意的要点。

    iPhone开发基础教程_学习笔记

    iPhone开发基础教程_学习笔记.docx

    iphone开发学习笔记(1)--NSLog

    iphone开发学习笔记(1)--NSLog

    iphone开发一年的工作笔记

    这份文档是我从事ios开发一年以来,总结工作中常用到的200余个ios小知识点,并添加可以调试观看的实例代码,方便iso开发者学习和交流,弄清楚这些,基本从事ios开发没什么问题。

    IPhone开发常用技术笔记汇总

    本压缩包中包含了Iphone开发中常用到的技术总结笔记,五六十中技术方法以及季节方案,包括内存管理,方法回调,获取当前地点,自定义CELL,VIew圆角等等等,太多的奶水包,是我开发中所有的精华所在,只有你不知道的...

    iPhone笔记-Object-c基础

    iPhone笔记-Object-c基础总结

    iphone笔记

    开发笔记,ios中关于入口类的相关方法的阐述,清晰的通俗

    iPhoneX安全区域与H5引发的问题(Safe Area)

    最近正在开发小程序相关的需求时,遇到了一个问题——ios高版本的安全区域,即iPhoneX及以上拥有的“刘海儿”等可滑动区域会与你开发的内容重合。 幸运的是,在小程序上我们只需要考虑底部的底部Home Indicator上滑...

    移动互联网开发笔记 VOL 2

    刊首语 征稿 在Android平台下定制系统的主屏幕 IPhone开发内存管理  Three20的使用 iOS开发者计划申请流程攻略 雅虎画报IPad版本的开发经验介绍

    Iphone开发资料整理

    Cocoa基本原理指南.pdf Dashcode_UserGuide.pdf iPhonei&Touch.doc Objective-C Beginner's Guide.pdf

    千锋OC源码,自学iphone开发

    千锋3g培训 OC源码以及课堂笔记 按日期分类 每天代码都有

    IOS开发 初级笔记

    ios开发的一些经验 和一些很使用的快捷键 适合初学者

    swift_study:详细信息!Swift iPhone App 开发入门笔记

    swift_study详细信息!Swift iPhone App 开发入门注的示例代码用于学习 Swift 开发

    使用XIB自定义一个UIView

    iPhone开发学习笔记005——使用XIB自定义一个UIView,然后将这个view添加到controller的view。

    timenote时光笔记(记事本软件) v2.37.zip

    TimeNote为珍惜时间,珍惜往事的人潜心开发的软件,一款具有独立文件与事件预测的跨平台日程管理软件(支持Android、IPhone与各PC平台)。本软件采用独家原创数据解码格式,支持云端异步操作,并能对普遍使用的ICS文件...

    objective c学习笔记和示例

    苹果的iPhone和iPad应用开发都指定使用同一开发语言:Objective-C语言。...Objective-C已经成为苹果系统下应用程序开发的标准程序设计语言,掌握了这一开发语言,你就能开始开发iPhone、iPad乃至Mac应用程序。

Global site tag (gtag.js) - Google Analytics