關於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];
}

發佈了20 篇原創文章 · 獲贊 3 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章