iPhone開發(fā)筆記和技巧總結(jié) (一)
作者:佚名
iPhone開發(fā)筆記和技巧總結(jié)。
1)iphone程序中實現(xiàn)截屏的一種方法
在iphone程序中實現(xiàn)截屏的一種方法:
//導入頭文件
#import QuartzCore/QuartzCore.h
//將整個self.view大小的圖層形式創(chuàng)建一張圖片image UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//然后將該圖片保存到圖片圖
UIImageWriteToSavedPhotosAlbum(image,self,nil,nil);
2)Objective-C 畫圖
1.顏色和字體
UIKit提供了UIColor和UIFont類來進行設置顏色和字體,
UIColor *redColor=【UIColor redColor】;
【redColor set】;//設置為紅色
UIFont *front=【UIFont systemFontOfSize:14.0】;//獲得系統(tǒng)字體
【myLable setFont:font】;//設置文本對象的字體
2.drawRect方法
對于畫圖,你首先需要重載drawRect方法,然后調(diào)用setNeedsDisplay方法讓系統(tǒng)畫圖:
-(void)drawRect:(CGRect)rect;//在rect指定的區(qū)域畫圖
-(void)setNeedsDisplay;//讓系統(tǒng)調(diào)用drawRect畫圖
3)延時函數(shù)和Timer的使用
延時函數(shù):
[NSThread sleepForTimeInterval:5.0]; //暫停5s.
Timer的使用:
NSTimer *connectionTimer; //timer對象
//實例化timer
self.connectionTimer=[NSTimerscheduledTimerWithTimeInterval:1.5 target:selfselector:@selector(timerFired:) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop]addTimer:self.connectionTimer forMode:NSDefaultRunLoopMode];
//用timer作為延時的一種方法
do{
[[NSRunLoopcurrentRunLoop]runUntilDate:[NSDatedateWithTimeIntervalSinceNow:1.0]];
}while(!done);
//timer調(diào)用函數(shù)
-(void)timerFired:(NSTimer *)timer{
done =YES;
}
4)啟動界面的制作
iPhone開發(fā)實現(xiàn)splash畫面非常簡單,做一個全屏的歡迎頁的圖片,把它命名為Default.png,然后放在Xcode工程的Resource里面。
在XXXAppDelegate.m程序中,插入如下代碼:
- (BOOL)application:(UIApplication*)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//–inserta delay of 5 seconds before the splash screendisappears–
[NSThread sleepForTimeInterval:5.0];
//Override point for customization after applicationlaunch.
//Add the view controller’s view to the window anddisplay.
[windowaddSubview:viewController.view];
[windowmakeKeyAndVisible];
return YES;
}
這樣splash頁面就停留5秒后,消失了。
5)關于控制器Controller的思考
iPhone開發(fā)中,只有一個窗口,對應的是多個視圖,而視圖的組織形式各種各樣,關鍵是要靠控制器來組織各個視圖的邏輯關系。大體的關系如下:
窗體---主控制器(比如說導航控制器),主控制器在窗體里面,拖動過去即可,在AppDelegate中寫相關變量的代碼---在主控制器下有別的控制器,比如視圖控制器,可以通過interfacebuilder來關聯(lián)根視圖什么的----視圖控制器相當于一個根視圖,可以調(diào)用其他的視圖---視圖中包含類文件(.h,.m)和圖形界面文件(.xib)(兩個之間必須關聯(lián)起來。)
6)翻頁效果
經(jīng)常看到iPhone的軟件向上向下翻頁面的效果,其實這個很簡單,已經(jīng)有封裝好的相關方法處理。
//首先設置動畫的相關參數(shù)
[UIView beginAnimations:@"Curl"context:nil];
[UIView setAnimationDuration:1.25]; //時間
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];//速度
//然后設置動畫的動作和目標視圖
[UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
參數(shù)UIViewAnimationTransitionCurlUp代表向上翻頁,如果向下的話UIViewAnimationTransitionCurlDown.
forView那把當前的視圖傳進去。
//***提交動畫
[UIView commitAnimations];
7)自定義按鈕
UIButton *Btn;CGRect frame; Btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; //按鈕的類型
[Btn setImage:[UIImage imageNamed:@“aaa.png”] forState:UIControlStateNormal];//設置按鈕圖片
Btn.tag = 10; frame.size.width = 59; //設置按鈕的寬度
frame.size.height = 59; //設置按鈕的高度
frame.origin.x =150; //設置按鈕的位置
frame.origin.y =260; [Btn setFrame:frame]; [Btn setBackgroundColor:[UIColor clearColor]]; [Btn addTarget:self action:@selector(btnPressed:)forControlEvents:UIControlEventTouchUpInside]; //按鈕的單擊事件
[self.view addSubview:Btn]; [Btn release];-(void)btnPressed:(id)sender {//在這里實現(xiàn)按鈕的單擊事件}
8)截取屏幕圖片
//創(chuàng)建一個基于位圖的圖形上下文并指定大小為CGSizeMake(200,400)
UIGraphicsBeginImageContext(CGSizeMake(200,400));
//renderInContext 呈現(xiàn)接受者及其子范圍到指定的上下文
[self.view.layerrenderInContext:UIGraphicsGetCurrentContext()];
//返回一個基于當前圖形上下文的圖片
UIImage *aImage =UIGraphicsGetImageFromCurrentImageContext();
//移除棧頂?shù)幕诋斍拔粓D的圖形上下文
UIGraphicsEndImageContext();
//以png格式返回指定圖片的數(shù)據(jù)
imageData = UIImagePNGRepresentation(aImage);
【編輯推薦】
責任編輯:冰凝兒
來源:
博客園