iphone常用代码块

User Experience Coding How-To's

 

view的子视图不显示超出父视图的属性 clipsTobounds

5分隔字符串Quickies for NSString

NSString *string = @"oop:ack:bork:greeble:ponies";
NSArray *chunks = [string componentsSeparatedByString: @":"];
 6 @selector中的方法中的冒号[摘抄

7uifont[link

UIFont *mainTitleFont = [UIFont boldSystemFontOfSize:14.0];
UIFont *subTitleFont  = [UIFont SystemFontOfSize:14.0];
UIFont *textFont      = [UIFont italicSystemFontOfSize:12.0];

8iPhone中缩放图片

http://www.xw81.com/Html/Article/iPhone/iPhone-scale-image.html
 

from 勇者之尊 start

9将图片从左到右翻页效果显示[一个位置到另外一个位置,设置过度时间]
    UIImageView *imageView = [[UIImageView alloc] 
    initWithFrame:CGRectMake(0, 0, 0, 470)];
    [imageView setImage:[UIImage imageNamed:@"Bg.jpg"]];
    self.myImageView =imageView;
    [self.view addSubview:imageView];
    [imageView release];
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.5];
    [myImageView setFrame:CGRectMake(0, 0, 310, 470)];   
    [UIView commitAnimations];
 
10让覆盖在下面层的视图接受触摸事件
searchImage.exclusiveTouch = YES;//第一层
searchImage.userInteractionEnabled = NO;
myMapView.exclusiveTouch = NO;//第二层
myMapView.userInteractionEnabled = YES;
 
11.View的缩放
NSValue *touchPointValue = [[NSValue valueWithCGPoint:CGPointMake(100,100)] retain];
[UIView beginAnimations:nil context:touchPointValue];
transform = CGAffineTransformMakeScale(0.1,0.21);
firstPieceView.transform = transform;
[UIView commitAnimations];   
 

12更改cell选中的背景
    UIView *myview = [[UIView alloc] init];
    myview.frame = CGRectMake(0, 0, 320, 47);
    myview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"0006.png"]];
    cell.selectedBackgroundView = myview;

13从本地加载图片
NSString *boundle = [[NSBundle mainBundle] resourcePath];
[web1 loadHTMLString:[NSString stringWithFormat:@"<img src='http://fei263.blog.163.com/blog/0001.png'/>"] baseURL:[NSURL fileURLWithPath:boundle]];
14 从网页加载图片并让图片在规定长宽中缩小
[cell.img loadHTMLString:[NSString stringWithFormat:@"<html><body><img src='http://fei263.blog.163.com/blog/%@' height='90px' width='90px'></body></html>",goodsInfo.GoodsImg] baseURL:nil];
将网页加载到webview上通过javascript获取里面的数据,如果只是发送了一个连接请求获取到源码以后可以用正则表达式进行获取数据
NSString *javaScript1 = @"document.getElementsByName('.u').item(0).value";
NSString *javaScript2 = @"document.getElementsByName('.challenge').item(0).value";
NSString *strResult1 = [NSString stringWithString:[theWebView stringByEvaluatingJavaScriptFromString:javaScript1]];
NSString *strResult2 = [NSString stringWithString:[theWebView stringByEvaluatingJavaScriptFromString:javaScript2]];

 

15 将一个指定的图形放大或缩小为指定的size
-(UIImage*)scaleToSize:(UIImage*)img size:(CGSize)size 

    // 创建一个bitmap的context 
    // 并把它设置成为当前正在使用的context 
    UIGraphicsBeginImageContext(size); 
    // 绘制改变大小的图片 
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    // 从当前context中创建一个改变大小后的图片 
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
    // 使当前的context出堆栈 
    UIGraphicsEndImageContext(); 
    // 返回新的改变大小后的图片 
    return scaledImage; 
}

from 勇者之尊 end
 16 异步
NSMutableData* buf = [[NSMutableData alloc] initWithLength:0];
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];  // 收到响应时, 会触发
- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)aResponse;
 // 每收到一次数据, 会调用一次
- (void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data
{[buf appendData:data];}.  // 网络错误时触发
- (void)connection:(NSURLConnection *)aConn didFailWithError:(NSError *)error;  // 全部数据接收完毕时触发
- (void)connectionDidFinishLoading:(NSURLConnection *)aConn;
2同步
// 初始化請求  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];     
// 設置URL [request setURL:[NSURL URLWithString:urlStr]];
// 設置HTTP方法 [request setHTTPMethod:@"GET"];
// 發送同步請求, 這裡得returnData就是返回得數據楽
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; // 釋放對象 [request release];

17图片旋转

CGAffineTransform rotate = CGAffineTransformMakeRotation( 1.0 / 180.0 * 3.14 );
[imageView setTransform:rotate];

加速器

iPhone Coding: Using the Accelerometer

随机颜色 

 CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX;

 CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;

 CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;

 return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f]; 

 

下文件之前获取到文件大小的代码

[m_pASIHTTPRequest setDidReceiveResponseHeadersSelector:@selector(didReceiveResponseHeaders:)];
- (void)didReceiveResponseHeaders:(ASIHTTPRequest *)request
{
    NSLog(@"didReceiveResponseHeaders %@",[m_request.responseHeaders valueForKey:@"Content-Length"]);

Handing the Keyboard notifications

 计算字符串的长度

UIFont *f =[UIFont systemFontSize];

CGSize stringSize = [aString sizeWithFont:font];

更改UISwitch上的text

为要更改Text的UISwitch建立IB连接:例如mySwitch,在代码中改变文字:

((UILabel *)[[[[[[self.mySwitch subviewslastObjectsubviewsobjectAtIndex:2] subviewsobjectAtIndex:0]).text = @"Foo";

((UILabel *)[[[[[[self.mySwitch subviewslastObjectsubviewsobjectAtIndex:2] subviewsobjectAtIndex:1]).text = @"Bar"

 

 

 


posted @ 2010-10-10 09:52  HA-LOU  阅读(548)  评论(0编辑  收藏  举报