iphone开发常用代码
更改cell选中的背景
Java代码
//创建一个UIView对象
UIView *myview = init];
//设置UIView对象的外观大小
myview.frame = CGRectMake(0, 0, 320, 47);
//设置UIView对象的背景色。 ] 从图片中创建颜色
myview.backgroundColor = ];
//设置cell被选中时的颜色
cell.selectedBackgroundView = myview;
在数字键盘上添加button:
Java代码
//定义一个消息中心
//addObserver:注册一个观察员 name:消息名称
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
//定义的动作
- (void)keyboardWillShow:(NSNotification )note {
// 创建一个自定义的UIButton对象
UIButton doneButton = ;
//设置UIButton大小的外观大小
doneButton.frame = CGRectMake(0, 163, 106, 53);
//设置该UIButton对象在正常状态下显示的图片
forState:UIControlStateNormal];
//注册事件
;
// locate keyboard view取得UIWindow对象
UIWindow tempWindow = windows] objectAtIndex:1];UIView keyboard;
//遍历window上的所有subview
for(int i=0; i<; i++)
{
keyboard = ;
// keyboard view found; add the custom button to it //如果该subview是键盘,就在该键盘上添加创建的button 对象
if( hasPrefix:@"<UIKeyboard"] == YES)
;
}
}
正则表达式使用:
被用于正则表达式的字串必须是可变长的,不然会出问题
将一个空间放在视图之上.above:在什么之上的意思 Java代码
;
从本地加载图片
Java代码
//取得当前应用程序的束--
//取得资源的路径
NSString *boundle = resourcePath];
baseURL:];
从网页加载图片并让图片在规定长宽中缩小
Java代码
baseURL:nil
];
将网页加载到webview上通过javascript获取里面的数据, 如果只是发送了一个连接请求获取到源码以后可以用正则表达式进行获取数据 C代码
- NSString *javaScript1 = @"document.getElementsByName('.u').item(0).value";
- NSString *javaScript2 = @"document.getElementsByName('.challenge').item(0).value";
- NSString *strResult1 = ];
- NSString *strResult2 = ];
用NSString怎么把UTF8转换成unicode
Java代码
utf8Str //
NSString *unicodeStr = encoding:NSUnicodeStringEncoding];
View自己调用自己的方法:
Java代码
//黄色段为方法名,和延迟几秒执行.loginToNext:方法名称 。afterDelay:2--延长2秒调用
;
显示图像:
Java代码
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = initWithFrame:myImageRect];
];
//opaque是否透明
myImage.opaque = YES;
;
;
WebView:
Java代码
CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView = initWithFrame:webFrame];
];
NSString *urlAddress = @"http://www.google.com";
NSURL *url = ;
NSURLRequest *requestObj = ;
;
;
;
显示网络活动状态指示符 这是在iPhone左上部的状态栏显示的转动的图标指示有背景发生网络的活动。
Java代码
UIApplication* app = ;
app.networkActivityIndicatorVisible = YES;
动画:一个接一个地显示一系列的图象
Java代码
NSArray *myImages = ,
,
,
,
nil];
//创建一个UIImageView对象
UIImageView *myAnimatedView = ;
//初始化UIImageView对象的大小
];
//animationImages属性返回一个存放动画图片的数组
myAnimatedView.animationImages = myImages;
//浏览整个图片一次所用的时间
myAnimatedView.animationDuration = 0.25;
// 0 = loops forever 动画重复次数
myAnimatedView.animationRepeatCount = 0;
//开始动画
;
//把该UIImageView对象添加到view视图中
;
//释放
;
动画:显示了something在屏幕上移动。
注:这种类型的动画是“开始后不处理” -你不能获取任何有关物体在动画中的信息(如当前的位置) 。
如果您需要此信息,您会手动使用定时器去调整动画的X和Y坐标
这个需要导入QuartzCore.framework
Java代码
CABasicAnimation *theAnimation;
theAnimation=
;
//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=;
theAnimation.toValue=;
;
Draggable items//拖动项目 Here's how to create a simple draggable image.//这是如何生成一个简单的拖动图象 Java代码
-
Create a new class that inherits from UIImageView
@interface myDraggableImage : UIImageView
{
} -
In the implementation for this new class, add the 2 methods:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
// Retrieve the touch point 检索接触点
CGPoint pt = locationInView:self];
startLocation = pt;
bringSubviewToFront:self];
}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
// Move relative to the original touch point 相对以前的触摸点进行移动
CGPoint pt = locationInView:self];
CGRect frame = ;
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
;
}
-
Now instantiate the new class as you would any other new image and add it to your view
//实例这个新的类,放到你需要新的图片放到你的视图上dragger = initWithFrame:myDragRect];
];
;
线程:
Java代码
- Create the new thread:
;
- Create the method that is called by the new thread:
- (void)myMethod
{
NSAutoreleasePool *pool = init];
*** code that should be run in the new thread goes here ***
;
}
/***What if you need to do something to the main thread from inside your new thread
*(for example, show a loading //symbol)? Use *performSelectorOnMainThread.
**/
;
// Look in Documents for an existing plist file
Java代码
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ;
myPlistPath =
];
;
// If it's not there, copy it from the bundle
Java代码
NSFileManager *fileManger = ;
if ( ! )
{
NSString *pathToSettingsInBundle =
pathForResource:plistName
ofType:
@"plist"];
}
//Now read the plist file from Documents
Java代码
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = ;
NSString *path = ;
NSMutableDictionary *plist = ;
//Now read and set key/values
Java代码
myKey = (int) intValue];
myKey2 = (bool) boolValue];
;
;
//Alerts Show a simple alert with OK button.
Java代码
UIAlertView *alert = initWithTitle:nil message: @"An Alert!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil ];
;
;
//Info button //Increase the touchable area on the Info button, so it's easier to press.
Java代码
CGRect newInfoButtonRect = CGRectMake(infoButton.frame.origin.x-25, infoButton.frame.origin.y-25,infoButton.frame.size.width+50,infoButton.frame.size.height+50);
;
//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代码
for (UIImageView *anImage in )
{
if (anImage.tag == 1)
{
// do something
}
}
原文地址:http://o0o0o0o.iteye.com/blog/623459