关于Iphone开发得一些案例及常用知识

tabBar透明的效果
http://www.cocoachina.com/bbs/read.php?tid=17815

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];

--------------------------

设置Table Cell的背景图的公用类代码
http://www.cocoachina.com/downloads/video/2010/0521/1531.html

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface UITableViewCell (UITableViewCellExt)

- (void)setBackgroundImage:(UIImage*)image;
- (void)setBackgroundImageByName:(NSString*)imageName;

@end


#import "UITableViewCellExt.h"


@implementation UITableViewCell (UITableViewCellExt)

- (void)setBackgroundImage:(UIImage*)image
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.contentMode = UIViewContentModeCenter;
    self.backgroundView = imageView;
    [imageView release];
    
}

- (void)setBackgroundImageByName:(NSString*)imageName
{
    [self setBackgroundImage:[UIImage imageNamed:imageName]];
}
@end

调 用示例:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [cell setBackgroundImageByName:@"text-background.png"];
        
        
    }
    
    return cell;
}

-------------------------------------
iPhone SDK 解析 xml的官方示例代码
http://www.cocoachina.com/downloads/video/2010/0520/1523.html

----------------------------------------------

把图片切成圆角代码
http://www.cocoachina.com/bbs/read.php?tid-1757.html

static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,
                 float ovalHeight)
{
    float fw, fh;
    if (ovalWidth == 0 || ovalHeight == 0) {
    CGContextAddRect(context, rect);
    return;
    }
    
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
    CGContextScaleCTM(context, ovalWidth, ovalHeight);
    fw = CGRectGetWidth(rect) / ovalWidth;
    fh = CGRectGetHeight(rect) / ovalHeight;
    
    CGContextMoveToPoint(context, fw, fh/2);  // Start at lower right corner
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);  // Top right corner
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right
    
    CGContextClosePath(context);
    CGContextRestoreGState(context);
}


+ (id) createRoundedRectImage:(UIImage*)image size:(CGSize)size
{
    // the size of CGContextRef
    int w = size.width;
    int h = size.height;
    
    UIImage *img = image;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGRect rect = CGRectMake(0, 0, w, h);
    
    CGContextBeginPath(context);
    addRoundedRectToPath(context, rect, 10, 10);
    CGContextClosePath(context);
    CGContextClip(context);
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return [UIImage imageWithCGImage:imageMasked];
}

直接调用 createRoundedRectImage....
返回圆角图片
圆角大小自行修改 CGContextAddArcToPoint....

-------------------------------
检测iPhone/iPod Touch/iPad设备类型
http://www.cocoachina.com/bbs/read.php?tid-20994.html

-------------------------------------
iPhone上气泡式聊天的代码
http://www.cocoachina.com/downloads/video/2010/0510/1379.html

聊天程序--(UDP通信,bubble代码)
http://www.cocoachina.com/bbs/read.php?tid-24324-fpage-2.html

第一个iphone小程序(实现聊天功能)
http://www.cocoachina.com/bbs/read.php?tid-24956-fpage-2.html
----------------------------------------
ExpanderController可伸缩框架的代码
http://www.cocoachina.com/downloads/video/2010/0510/1368.html
-------------------------------------------
一段模拟水波纹的代码,希望对大家有用
http://www.cocoachina.com/downloads/video/2010/0506/1351.html
-------------------------------
根据经纬度计算两点之间距离的Obcective-C代码
http://www.cocoachina.com/downloads/video/2010/0506/1344.html

其中其中er 就是地球椭球半径,对于google map使用 6378137 就可以了。函数的调用非常简单,几乎使用任何平台:)
#define PI 3.1415926
double LantitudeLongitudeDist(double lon1,double lat1,
                              double lon2,double lat2)
{
    double er = 6378137; // 6378700.0f;
    //ave. radius = 6371.315 (someone said more accurate is 6366.707)
    //equatorial radius = 6378.388
    //nautical mile = 1.15078
    double radlat1 = PI*lat1/180.0f;
    double radlat2 = PI*lat2/180.0f;
    //now long.
    double radlong1 = PI*lon1/180.0f;
    double radlong2 = PI*lon2/180.0f;
    if( radlat1 < 0 ) radlat1 = PI/2 + fabs(radlat1);// south
    if( radlat1 > 0 ) radlat1 = PI/2 - fabs(radlat1);// north
    if( radlong1 < 0 ) radlong1 = PI*2 - fabs(radlong1);//west
    if( radlat2 < 0 ) radlat2 = PI/2 + fabs(radlat2);// south
    if( radlat2 > 0 ) radlat2 = PI/2 - fabs(radlat2);// north
    if( radlong2 < 0 ) radlong2 = PI*2 - fabs(radlong2);// west
    //spherical coordinates x=r*cos(ag)sin(at), y=r*sin(ag)*sin(at), z=r*cos(at)
    //zero ag is up so reverse lat
    double x1 = er * cos(radlong1) * sin(radlat1);
    double y1 = er * sin(radlong1) * sin(radlat1);
    double z1 = er * cos(radlat1);
    double x2 = er * cos(radlong2) * sin(radlat2);
    double y2 = er * sin(radlong2) * sin(radlat2);
    double z2 = er * cos(radlat2);
    double d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
    //side, side, side, law of cosines and arccos
    double theta = acos((er*er+er*er-d*d)/(2*er*er));
    double dist  = theta*er;
    return dist;
}
----------------------------
巴黎自行车信息查询软件源码
http://www.cocoachina.com/downloads/video/2010/0429/1266.html
--------------------------------
自制 iPhone DataGrid 数据列表组件,支持行列锁定
http://www.cocoachina.com/downloads/video/2010/0429/1267.html
--------------------------------
iPhone播放本地视频的代码
http://www.cocoachina.com/downloads/video/2010/0428/1260.html
----------------------------
UIWebView里面长按一个链接后自定义弹出菜单
http://www.cocoachina.com/iphonedev/sdk/2010/0716/1879.html
-------------------------------------------
iPhone上画面切换特效及代码
http://www.cocoachina.com/downloads/video/2010/0419/1123.html
-------------------------
永远的扫雷英雄(开源) 登场
http://www.cocoachina.com/bbs/read.php?tid-12818.html
--------------------------------
在iPhone中实现图片缩放
http://www.cocoachina.com/iphonedev/toolthain/2009/0611/198.html
-----------------
创建iPhone锁定划动条的方法
http://www.cocoachina.com/iphonedev/toolthain/2009/0611/224.html
-------------
UICoverFlowLayer例子:制作iPhone的Cover Flow效果
http://www.cocoachina.com/iphonedev/toolthain/2009/0611/196.html
-----------------------------
如何在iPhone程序读取数据时显示进度窗
http://www.cocoachina.com/iphonedev/toolthain/2009/0611/173.html
-----------------
UITabBarController 保存调整后的more选项(增加所有TabBar之间切换).
http://www.cocoachina.com/bbs/read.php?tid-12424.html
--------------------------------------------
NSStirng、NSArray、以及枚举(Method小集合)
http://www.cocoachina.com/bbs/read.php?tid-7735.html
------------
UIview动画
http://www.cocoachina.com/bbs/read.php?tid-8035-keyword-demo.html

http://www.cocoachina.com/bbs/read.php?tid-11343.html
----------------
搜索功能
http://www.cocoachina.com/bbs/read.php?tid-5869.html

-------------
UILabel显示换行的方法
UILabel*label;

//设置换行
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;

换行符还是/n
比如NSString * xstring=@"lineone/nlinetwo"

记得要把label的高度设置的足够显示多行内容。

------------------
手把手教你做iphone的soap应用(webservice)
http://www.cocoachina.com/bbs/read.php?tid-16561-fpage-2.html
---------------------------
点击Table一行弹出下拉一排按钮的TableViewCell类
http://www.cocoachina.com/bbs/read.php?tid-23065-fpage-2.html

http://www.cocoachina.com/bbs/read.php?tid=21494&page=1#131711
-----------------
一些iPhone开源项目代码
http://www.cocoachina.com/bbs/read.php?tid-4532-fpage-2.htm
----------------------
<iOS4>后台运行(Multitasking)以及本地通知(Local Notifications) 有图,有书,有代码,统一打包
http://www.cocoachina.com/bbs/read.php?tid-20423-fpage-2.html
-------------------
开源一个基于MultiTouch事件图片移动和缩放的demo
http://www.cocoachina.com/bbs/read.php?tid-25387-fpage-2.html
-------------------------


 使用NSClassFromString

NSClassFromString是一个很有用的东西,尤其在进行iPhone toolchain的开发上。

正常来说,
id myObj = [[NSClassFromString(@"MySpecialClass") alloc] init];

id myObj = [[MySpecialClass alloc] init];
是一样的。但是,如果你的程序中并不存在MySpecialClass这个类,下面的写法会出错,而上面的写法只是返回一个空对象而已。
因此,在某些情况下,可以使用NSClassFromString来进行你不确定的类的初始化。

比如在iPhone中,NSTask可能就会出现这种情况,所以在你需要使用NSTask时,最好使用:
[[NSClassFromString(@"NSTask") …..]]
而不要直接使用[NSTask …]这种写法。

NSClassFromString的好处是:
1 弱化连接,因此并不会把没有的Framework也link到程序中。
2 不需要使用import,因为类是动态加载的,只要存在就可以加载。因此如果你的toolchain中没有某个类的头文件定义,而你确信这个类是可以用的,那么也可以用这种方法。



在使用NSArray时出现 EXC_BAD_ACCESS错误
因为NSArray与NSMutableArray不一样,它不需要初始化分配内存空间,它的赋值可以直接由一个数组传递给它,所以在赋值时要调用属性即在数组前加self.这样它的引用计数就会加1才能保持数据的准确性。

 

 

NSData 对象是不可变的,它被创建后就不能改变。NSMutableData支持正数据内容中添加和删除字节
*/
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

const char *string=”hi there,this is a c string”;
//NSData 包装了大量的字节,可以获得数据的长度和指向字节起始位置的指针。
//如果将数据块传递给一个函数或方法,可以通过传递一个自动释放的NSData来实现,无需担心内存清除问题
//strlen(string)+1,它用于包含c字符串的尾部的零字节,通过包含零字节,可以使用%s格式的说明符输出字符串
NSData *data=[NSData dataWithBytes:string length:strlen(string)+1];
NSLog(@”——%@”,data);

NSLog(@”length=%d,string=’%s’”,[data length],[data bytes]);
//atomically:设置为true或者yes,好像没有什么区别
[data writeToFile:@"/tmp/ss.txt" atomically:TRUE];

[pool drain];
return 0;
}

random()函数的使用介绍

1、首先要让大家知道的是,random()在程序中调用是按时间来进行排序的,从你开始调用random()函数起程序就按照时间进行顺序的产生随机数,每次应用程序开始,时间都会重置,故会出现每次开启应用程序,随机数虽然时随机的,但是顺序时固定的,大家应该先知道这个原理
2、如何让一个random()函数在每次开启程序时无顺序呢?小弟不才,结合OpenGL中的原理,进行了尝试。在你调用random()函数之前,首先写一个方法,该方法为:srandom(time(NULL));
该方法的意思就是让以后的随机数不再按时间进行排序,此后你如果再使用random()方法便不用担心它的顺序随机了。
例:

  1. srandom(time(NULL));
  2. for(int i = 0; i<10; i++){
  3. NSLog(@”%d”,random());

试一下,看看其结果,是不是不再顺序随机了。
3、说到随机数,我还有些研究,随机数不仅用random(),还可以使用rand(),同样有srand(time(NULL));但是,在不使用 srand(time(NULL))之前,rand()的第一个随机值是16807,而random()的随机值第一个随机值是1804289383;这就是说程序默认的随机数调用的是srand(1)或者srandom(1);你如果设置一下为srand(2),它第一个随机数便不再是16807,而是 33614,设为srand(3),则第一个随机数便是50421。这只是srand(..)的情况,如果是srandom(..),则无规律可谈。

iPhon开发-移除UIView中的所有对象

循环移除UIView中的所有对象:
for (UIView *sub in [self.view subviews]){
[sub removeFromSuperview];
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值